压态下的涟漪效应+正常状态下的透明度

时间:2015-05-13 13:03:47

标签: android animation layout imageview ripple

我希望ViewGroup中的ImageView在按下时绘制波纹并且这是正常的!但是当按下ViewGroup时,其中的ImageView应该保持透明,否则ImageView背景可见:(实际上看到的颜色为alpha-orange,是按下时的纹波)。 enter image description here

只有设备API 21+才会发生这种情况。使用设备API< 21时,我使用选择器,当按下ViewGroup时,图像背景保持透明。

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/item_detail_field"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/margin_left"
    android:paddingStart="@dimen/margin_left"
    android:paddingTop="@dimen/margin_top_item_field"
    android:paddingBottom="@dimen/margin_bottom"
    android:baselineAligned="false"
    android:background="@drawable/layout"
    android:stateListAnimator="@anim/touch_raise"
    android:orientation="horizontal">

...

    <ImageView
        android:id="@+id/item_detail_showfield_icon"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/button_mini_oval"
        android:stateListAnimator="@anim/touch_raise"
        android:clickable="true"
        android:contentDescription="@null"
        android:scaleType="center"
        android:visibility="invisible"
        android:src="@drawable/show_button"/>

...

</LinearLayout>

抽拉/ button_mini_oval.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@color/primary_highlight" />
        </shape>
    </item>
    <item>
        <shape android:shape="oval">
            <solid android:color="@android:color/transparent" />
        </shape>
    </item>
</selector>

绘制-V21 / button_mini_oval.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_shortAnimTime"
    android:color="?android:colorControlHighlight">

    <item android:state_pressed="true">
        <shape android:shape="oval">
            <solid android:color="@android:color/white" />
        </shape>
    </item>
    <item>
        <color android:color="@android:color/transparent" />
    </item>
</ripple>

我在ripple xml中尝试了很多配置,添加<selector> 它可以(见下文),但是如果你只是点击ImageView,则纹波不可见(波纹动画似乎被切断了),只有你一直按下,你才会看到涟漪......

在正常状态下,压力状态下是否有波纹和透明是什么组合?

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_shortAnimTime"
    android:color="?android:colorControlHighlight">

    <item>
        <selector>
            <item android:state_pressed="true">
                <shape android:shape="oval">
                    <solid android:color="@android:color/white" />
                </shape>
            </item>
            <item>
                <color android:color="@android:color/transparent" />
            </item>
        </selector>
    </item>
</ripple>

2 个答案:

答案 0 :(得分:5)

删除android:state_pressed="true",我找到了2个解决方案,但在这两种情况下我都丢失了 touch_raise 动画(为什么?):

带有蒙面纹波:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_shortAnimTime"
    android:color="?android:colorControlHighlight">

    <item android:id="@android:id/mask">
        <selector>
            <item>
                <shape android:shape="oval">
                    <solid android:color="@android:color/white" />
                </shape>
            </item>
            <item>
                <color android:color="@android:color/transparent" />
            </item>
        </selector>
    </item>
</ripple>

带有未屏蔽的小纹波(比1好):

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_shortAnimTime"
    android:color="?android:colorControlHighlight">

    <selector>
        <item>
            <color android:color="@android:color/transparent" />
        </item>
    </selector>
</ripple>

我不知道为什么添加<selector>以便在透明背景上产生涟漪就足够了!!! 很多人都试图实现这一目标,但它不是'谷歌的意图,相反,这种情况非常有用。

答案 1 :(得分:5)

对我来说,它有效

<?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="@color/white"
    android:radius="3dp">
        <item android:drawable="@drawable/transparent_ripple_button"/>
</ripple>

和单独的

  

transparent_ripple_button.xml

在您的文件夹中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/black_12" />
            <stroke android:width="0.2dp" android:color="@color/white" />
            <corners android:radius="3dp" />
        </shape>
    </item>

    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/black_12" />
            <stroke android:width="0.2dp" android:color="@color/white" />
            <corners android:radius="3dp" />
        </shape>
    </item>

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent" />
            <stroke android:width="0.2dp" android:color="@color/white" />
            <corners android:radius="3dp" />
        </shape>
    </item>

</selector>