Android 波纹效应 + 非按钮 View 的高度

标签 android android-5.0-lollipop rippledrawable android-elevation

我正在尝试向 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 定位(与按钮可绘制对象相反)。

添加波纹效果或动画单独工作,只要 View 的背景根据 this 没有透明度。问题。

我不确定这是否与上述问题相关,但看到标准按钮设法同时使用波纹和高度动画反馈,我认为在其他 View 上也可以实现这种效果。

任何对此问题的见解都将不胜感激。

最佳答案

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

结果:

enter image description here

编辑:ForegroundLinearLayout 存在于支持设计库中。所以你可以使用 android.support.design.internal.ForegroundLinearLayout

关于Android 波纹效应 + 非按钮 View 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28192377/

相关文章:

Android imagebutton 波纹效果

android - 同心旋转叠加图像

android - 为旧版本的 Android 覆盖 Android-L CardView state_pressed

java - 找不到类 'android.graphics.drawable.RippleDrawable' Unicode 问题?

android - 更改 Lollipop 操作栏的操作栏项目的按下颜色

android-5.0-lollipop - ActionBar 中动画 ActionBarDrawerToggle 的颜色

android - 如何在 View 内的特定位置触发 Android Lollipop 的涟漪效应,而不触发触摸事件?

java - FragmentTransaction 引起的 Android Studio NullPointerException

android - 在android中使AlertActivity全屏

android studio 3.0.1 永远在 android profiler 中获取内存分配的结果