本文章使用Android5.0中其中一个炫酷的效果,点击水波纹扩散效果( Ripple Effect).同时解决低于5.0版本的兼容问题.
让你半分钟搞定这个效果同时解决掉低版本系统不兼容问题.
先将水波纹图片展示,这个是自己截图比较简陋的:
首先工程目录截图:
主界面activity_main.xml中定义一个button:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.lsh.shuibowenview.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bt_sbw"/>
</RelativeLayout>
新建文件夹drawable-v21并且新建bt-sbw.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="#fff"
>
<item android:drawable="@drawable/bt_long_pre"></item>
</ripple>
因为这个ripple是Android5.0自带的水波纹效果,所以如果不建立这样的文件夹,程序安装环境就会变成5.0系统以上才可以安装.为了解决水波纹在低版本上不兼容
所以就需要在drawable文件下建立一个bt-sbw.xml文件满足当低版本系统时的情况.
在drawable下建立的bt-sbw.xml文件(注意:名字相同里面代码不同)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:drawable="@drawable/bt_long_nor" android:state_pressed="false"></item>
<item android:drawable="@drawable/bt_long_pre" android:state_pressed="true"></item>
</selector>
上面的代码是为了手机版本不满足Android 5.0以上系统,而造成程序报错设置的保险选择样式的效果.这个效果可以自己定义.
图片bt-long-nor.png和 bt-long-pre.png自己找就行,水波纹使用的图片是
bt_long_pre
写这篇博客是网上的5.0系统水波纹效果只有在5.0的手机上才能运行,在低版本手机上会出现错误.本文章就是补充低版本时的效果显示,让功能添加完善.
源代码下载:http://pan.baidu.com/s/1byI6gI