android 按钮水波纹效果

本文介绍了 Android 5.0 引入的触摸反馈动画,并详细讲解了如何利用 Material 主题自带的波纹效果实现按钮的水波纹动画。文中提供了两种常用的方法:一是直接使用系统提供的波纹效果属性;二是自定义波纹效果的 drawable 背景文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 5.0以后。加入了触摸反馈动画。当你使用了Material主题后,波纹动画会自动应用在所有的控件上,我们当然可以来设置其属性来调整到我们需要的效果。

按钮波纹效果有很多种方法。介绍最常用的两种

第一种:Material自带水波纹

通过如下代码设置波纹的背景:

android:background="?android:attr/selectableItemBackground"波纹有边界

android:background="?android:attr/selectableItemBackgroundBorderless"波纹超出边界

为view添加波纹效果:
<RelativeLayout
                android:id="@+id/user_info_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:background="?android:attr/selectableItemBackground"
             >
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:contentDescription="@null"
                    android:paddingRight="15dip"
                     />
            </RelativeLayout>
2.第二种方法
2.1 添加一个普通的 ripple_bg_drawable.xml 背景文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#8cc476" />
    <corners android:radius="0dp" />
</shape>
2.2添加带波纹效果的背景文件 ripple_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#FF21272B">
    <item android:drawable="@drawable/ripple_bg_drawable" />
</ripple>



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值