您的位置:首页 > 移动开发 > Android开发

android ripple水波纹详解

2017-09-22 14:09 260 查看


Ripple是Material Design(材料设计)中的效果;

虽然在5.0的机型上,会自带Ripple点击效果,但是有时候需要自己更改点击效果;

使用Ripple的关键就是在android:background中设置;

使用方法也非常简单,在drawable中创建**.xml文件:

<!--在真是项目中都有自己的主题颜色,Ripple效果的颜色默认是灰色;
可通过android:color 这个属性改变涟漪波浪的颜色-->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#ffff0000"
tools:targetApi="lollipop">>
<!--下面设置颜色没有用,添加item以后会约束水波纹的范围-->
<item android:drawable="@android:color/white" />
</ripple>


直接在需要水波纹的按钮上面设置:

<!--直接设置android:background="@drawable/ripple_01"属性即可
记住必须添加android:clickable="true"属性,不然不生效-->
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="70dp"
android:background="@drawable/ripple_01"
android:clickable="true"
android:text="不限定边界" />


但是很多按钮需要自定义shape或者设置UI给的图片,在Ripple中设置图片也非常的简单:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#FF21272B">
<!--自定义的shape作为背景
也可添加一张图片作为背景-->
<item android:drawable="@drawable/tv_bg" />
</ripple>

shape文件tv_ba.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#d5d555" />
<corners android:radius="4dp" />
</shape>


点击打开链接下载源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: