【发布时间】:2015-03-27 08:38:51
【问题描述】:
我正在尝试向 LinearLayout 添加触摸反馈,该反馈类似于 API 级别 21 中的常规按钮反馈,与 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>
并使用了 Google 提供的 StateListAnimator here:
<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 问题,添加波纹效果或动画单独工作,只要视图的背景没有透明度。
我不确定这是否是与上述问题相关的问题,但鉴于标准按钮设法同时使用波纹和高度动画反馈,我认为在其他视图上也可以实现此效果。
任何对此问题的见解将不胜感激。
【问题讨论】:
-
如果您希望在用户点击您的
LinearLayout时应用涟漪效果,您需要使用适当的StateListDrawable,而不是直接引用您的<ripple>。 -
您现有的背景显示“立即显示涟漪”。这似乎是一个奇怪的选择。
Button的背景是StateListDrawable,用于正常、按下、禁用、聚焦等不同的图像。你需要做同样的事情。 -
我想我明白了,但奇怪的是,正如我在问题中提到的那样,如果我只指定波纹作为背景,它会按预期工作。
-
两者会产生什么影响?
-
没有任何效果
标签: android android-5.0-lollipop rippledrawable android-elevation