我正在尝试向LinearLayout添加触摸反馈,它类似于API21级别中的常规按钮反馈,非常类似于this示例,但到目前为止还没有成功。
我定义了一个标准的可绘制涟漪,如下所示:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<solid android:color="?android:colorAccent" />
</shape>
</item>
并使用谷歌提供的here的StateListAnimator
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true">
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="@dimen/touch_raise"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="@android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueTo="0dp"
android:valueType="floatType" />
</item>
在定义了动画师和可绘制的波纹之后,我将它们添加到我的LinearLayout中,如下所示:
<LinearLayout
android:id="@+id/linearLayout"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:background="@drawable/ripple"
android:stateListAnimator="@anim/touch_elevation">我的想法是将这个LinearLayout用作按钮,因为在其中插入各种类型的文本和处理ImageView定位对我来说要简单得多(而不是按钮可绘制部分)。
单独添加涟漪效果或动画可以正常工作,只要视图的背景按照this问题没有透明度即可。
我不确定这是否是与上面提到的问题相关的问题,但由于标准按钮设法使用了涟漪和立面动画反馈,我认为在其他视图上也可以实现这种效果。
对这个问题的任何见解都将非常感谢。
发布于 2016-09-22 16:52:03
使用ForegroundLinearLayout。它允许将波纹或任何其他可绘制的图形放在前景中。用同样的方法可以创建ForegroundRelativeLayout等(只需使这个类从RelativeLayout扩展)。
下面是您的stateListAnimator和ripple可绘制示例:
<your.package.ForegroundLinearLayout
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_centerInParent="true"
android:gravity="center"
android:foreground="@drawable/bg_ripple"
android:background="#fff"
android:clickable="true"
android:stateListAnimator="@animator/animator_elevation">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world!"/>
</your.package.ForegroundLinearLayout>结果:

编辑:ForegroundLinearLayout存在于支持设计库中。所以你可以只使用android.support.design.internal.ForegroundLinearLayout
https://stackoverflow.com/questions/28192377
复制相似问题