且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在$ P $连锁反应pssed状态+透明度处于正常状态

更新时间:2022-12-11 15:42:00

删除安卓STATE_ pressed =真正的,我发现2解决方案,但在这两种情况下我失去了的 touch_raise 的动画(为什么?):

随着蒙面纹波:

 <纹波的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:exitFadeDuration =@安卓整数/ config_shortAnimTime
    机器人:颜色=机器人:colorControlHighlight>    <项目机器人:ID =@机器人:ID /掩码>
        <选择>
            <项目>
                <形状机器人:形状=椭圆形>
                    [固体机器人:色=@机器人:彩色/白/>
                < /形状>
            < /项目>
            <项目>
                <彩色机器人:颜色=@机器人:彩色/透明/>
            < /项目>
        < /选择>
    < /项目>
< /纹波>

随着东窗事发纹波小(比更好1):

 <纹波的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:exitFadeDuration =@安卓整数/ config_shortAnimTime
    机器人:颜色=机器人:colorControlHighlight>    <选择>
        <项目>
            <彩色机器人:颜色=@机器人:彩色/透明/>
        < /项目>
    < /选择>
< /纹波>

我不知道为什么它足以增加<选择> 来有一个透明的背景涟漪!很多人试图实现这一点,但它不是谷歌的意图,相反,它是非常有用的是这样的情况。

I would like the ImageView in the ViewGroup, when pressed, draws the ripple and this is working! But when the ViewGroup is pressed, the ImageView inside it, should remain transparent otherwise the ImageView background is visible: (that color alpha-orange, you actually see, is the ripple when pressed).

This happens only with devices API 21+. With devices API <21 I use a selector and the image background remains transparent as wanted when the ViewGroup is pressed.

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>

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

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

I tried many configurations in the ripple xml, adding a <selector> it works (see below), but if you just tap the ImageView, the ripple is not visible (the ripple animation seems cut), only if you keep pressed you see the ripple...

What is the combination to have ripple in pressed state and transparent in normal state?

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

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.