如何使用支持库实现波纹动画? [英] How to achieve ripple animation using support library?

查看:85
本文介绍了如何使用支持库实现波纹动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在单击按钮时添加波纹动画.我在下面确实喜欢,但是它要求minSdKVersion到21.

I am trying to add a ripple animation on button click. I did like below but it requires minSdKVersion to 21.

ripple.xml

ripple.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="?android:colorControlHighlight">
    <item>
        <shape android:shape="rectangle">
            <solid android:color="?android:colorAccent" />
        </shape>
    </item>
</ripple>

按钮

<com.devspark.robototextview.widget.RobotoButton
    android:id="@+id/loginButton"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/ripple"
    android:text="@string/login_button" />

我想使其与设计库向后兼容.

I want to make it backward compatible with the design library.

这怎么办?

推荐答案

基本波动设置

  • 该视图中包含的涟漪图.
    android:background="?selectableItemBackground"

  • Ripples contained within the view.
    android:background="?selectableItemBackground"

超出视图范围的波纹:
android:background="?selectableItemBackgroundBorderless"

Ripples that extend beyond the view's bounds:
android:background="?selectableItemBackgroundBorderless"

此处,用于解析Java代码中的?(attr) xml引用.

Have a look here for resolving ?(attr) xml references in Java code.

支持库

  • 使用?attr:(或?速记)代替?android:attr引用支持库,因此可用于API 7.
  • Using ?attr: (or the ? shorthand) instead of ?android:attr references the support library, so is available back to API 7.

带有图片/背景的波纹

  • 要获得图像或背景并叠加波纹,最简单的解决方案是将View包裹在FrameLayout中,并用setForeground()setBackground()设置波纹.
  • To have a image or background and overlaying ripple the easiest solution is to wrap the View in a FrameLayout with the ripple set with setForeground() or setBackground().

老实说,否则没有干净的方法.

Honestly there is no clean way of doing this otherwise.

这篇关于如何使用支持库实现波纹动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆