我正在尝试给一个LinearLayout添加触摸反馈,使其与API 21级别下的常规按钮反馈类似,就像这个示例中的那样,但迄今为止未能成功。
我已经定义了一个标准的涟漪drawable,如下所示:
<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>
我使用了谷歌提供的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的定位。
单独添加涟漪效果或动画效果是可以的,只要视图的背景没有透明度,就像这个问题一样。
我不确定这是否与上述问题有关,但是既然标准按钮可以同时使用涟漪和高程动画反馈,我认为在其他视图上实现此效果也是可能的。
非常感谢您对此问题的任何见解。
Button
的背景是StateListDrawable
,用于在正常、按下、禁用、聚焦等状态下使用不同的图像。你需要做同样的事情。 - CommonsWare