使用带有ImageButton的不同形状的意外结果表明

时间:2015-05-26 10:47:40

标签: android android-selector android-imagebutton android-shape

我的简单目标是在不同的按钮状态下更改ImageButton的背景。所以,我所拥有的是

mylayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity"
    android:orientation="vertical"
    android:id="@+id/idBackground">
    ...
    <ImageButton
        android:id="@+id/btnCapture"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@drawable/ic_photo_selector"
        android:src="@drawable/ic_photo"
        android:padding="25dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
    ...
</RelativeLayout>

ic_photo_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape android:shape="oval" >

            <gradient
                android:centerColor="#3311ffed"
                android:centerX="0.5"
                android:centerY="0.5"
                android:endColor="#8011ffed"
                android:gradientRadius="150"
                android:startColor="#2711ffed"
                android:type="radial" />

            <stroke
                android:width="2dp"
                android:color="#3300DDFF" />

            <padding
                android:bottom="15dp"
                android:left="15dp"
                android:right="15dp"
                android:top="15dp" />

        </shape>
    </item>

    <item android:drawable="@drawable/capture_button_bg"/>

</selector>

capture_button_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <gradient
        android:centerColor="#1111ffed"
        android:centerX="0.5"
        android:centerY="0.5"
        android:endColor="#5E11ffed"
        android:gradientRadius="150"
        android:startColor="#0511ffed"
        android:type="radial" />

    <stroke
        android:width="2dp"
        android:color="#1100DDFF" />

    <padding
        android:bottom="15dp"
        android:left="15dp"
        android:right="15dp"
        android:top="15dp" />

</shape>

ic_photo.png (这是透明图片,但由于此网站有白色背景,我将其贴在黑色图层上)

enter image description here

因此,正如您所看到的,两种状态(按下和默认)之间的视觉差异应该是透明的。 但作为真实设备的设计模式显示了另一个结果

                        Default State              Pressed State
enter image description here enter image description here

我的思绪确实被封锁了。为什么默认按钮与按下的按钮有这样的色差?正如我所料,差异应该只是透明模式。

有什么想法吗?或者我的代码片段可能有问题吗?

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的方法,但这很奇怪。

上述行为的场合包括使用内联<shape>用于按下状态以及对默认状态的drawable的引用。

<强>解决方案:

1)将内联<shape>用于按下状态默认状态

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape android:shape="oval" >

            <gradient
                android:centerColor="#3311ffed"
                android:centerX="0.5"
                android:centerY="0.5"
                android:endColor="#8011ffed"
                android:gradientRadius="150"
                android:startColor="#2711ffed"
                android:type="radial" />

            <stroke
                android:width="2dp"
                android:color="#3300DDFF" />

            <padding
                android:bottom="15dp"
                android:left="15dp"
                android:right="15dp"
                android:top="15dp" />

        </shape>
    </item>

    <item>
        <shape android:shape="oval" >

            <gradient
                android:centerColor="#1111ffed"
                android:centerX="0.5"
                android:centerY="0.5"
                android:endColor="#5E11ffed"
                android:gradientRadius="150"
                android:startColor="#0511ffed"
                android:type="radial" />

            <stroke
                android:width="2dp"
                android:color="#1100DDFF" />

            <padding
                android:bottom="15dp"
                android:left="15dp"
                android:right="15dp"
                android:top="15dp" />

        </shape>
    </item>

</selector>

2)使用按下状态的drawable引用和默认状态的内联<shape>

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:drawable="@drawable/capture_button_bg_pressed"/>

    <item>
        <shape android:shape="oval" >

            <gradient
                android:centerColor="#1111ffed"
                android:centerX="0.5"
                android:centerY="0.5"
                android:endColor="#5E11ffed"
                android:gradientRadius="150"
                android:startColor="#0511ffed"
                android:type="radial" />

            <stroke
                android:width="2dp"
                android:color="#1100DDFF" />

            <padding
                android:bottom="15dp"
                android:left="15dp"
                android:right="15dp"
                android:top="15dp" />

        </shape>
    </item>

</selector>

<强> capture_button_bg_pressed.xml

<?xml version="1.0" encoding="utf-8"?>

            <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >

                <gradient
                    android:centerColor="#3311ffed"
                    android:centerX="0.5"
                    android:centerY="0.5"
                    android:endColor="#8011ffed"
                    android:gradientRadius="150"
                    android:startColor="#2711ffed"
                    android:type="radial" />

                <stroke
                    android:width="2dp"
                    android:color="#3300DDFF" />

                <padding
                    android:bottom="15dp"
                    android:left="15dp"
                    android:right="15dp"
                    android:top="15dp" />

            </shape>

此外,重点是默认状态应该遵循最后一个(低于任何其他状态)。

我无法解释为什么会这样,但它对我有用。