使用具有 ImageButton 状态的不同形状时出现意外结果

Unexpected result on using the different shapes with an ImageButton states

我的简单目标是在不同的按钮状态下更改 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(是透明图,但是由于本站是白色背景,所以我把它贴在了黑色图层上)

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

                        Default State              Pressed State

我的脑子真的很堵。为什么默认按钮与按下的按钮有这样的色差?正如我所料,区别应该只是在透明模式下。

有什么想法吗?还是我的代码片段有问题?

我找到了我的问题的解决方案,但有些奇怪。

上述行为的发生是因为 按下状态 使用内联 <shape> 默认状态对可绘制对象的引用.

解法:

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> 16=]

<?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>

此外,要点是默认状态应该最后(在任何其他状态之后)。

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