我试图将触摸反馈添加到LinearLayout,类似于API级别21中常规Button的反馈,非常类似于
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定位(而不是按钮drawables).
只要视图的背景根据this问题没有透明度,就可以单独添加涟漪效果或动画.
我不确定这是否是与上述问题相关的问题,但看到标准按钮设法同时采用波纹和高程动画反馈,我认为在其他视图中也可以实现这种效果.
任何洞察这个问题将不胜感激.
解决方法
使用
ForegroundLinearLayout.它允许在视图的前景中放置波纹或任何其他可绘制的.您可以使用相同的方法创建ForegroundRelativeLayout等(只需使此类从RelativeLayout扩展).
以下是stateListAnimator和ripple drawable的示例:
<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