Ripple effect in pressed state + transparency in normal state

隐身守侯 提交于 2019-12-04 12:11:19

Removing android:state_pressed="true", I've found 2 solutions but in both cases I lost the touch_raise animation (why?):

With masked ripple:

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

With unmasked little ripple (nicer than 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>

I don't know why it is sufficient to add <selector> to have a ripple on a transparent background!!! a lot of people were trying to achieve this but it wasn't the intention of Google, instead it is very useful is such situations.

With me, it works

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

and a separate

transparent_ripple_button.xml

in your folder

<?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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!