在Android 5.0中加入了触摸反馈动画。其中最具代表性的就是波纹动画,点击按钮时会从点击的位置产生类似于波纹的扩散效果。当你使用了Material主题后,波纹动画会自动应用在所有的控件上,可以通过设置其属性来调整到我们需要的效果。
可以通过如下代码设置波纹的背景:
android:background="?android:attr/selectableItemBackground"波纹有边界
android:background="?android:attr/selectableItemBackgroundBorderless"波纹超出边界
使用效果如下:
B1是不设任何背景的按钮
B2设置了?android:attr/selectableItemBackground
B3设置了?android:attr/selectableItemBackgroundBorderless
设置颜色
我们也可以通过设置xml属性来调节动画颜色,从而可以适应不同的主题:
android:colorControlHighlight:设置波纹颜色
android:colorAccent:设置checkbox等控件的选中颜色
在android5.0以下的想要实现这种波纹效果得借助第三方库:
RippleEffect。
Gtihub地址为:
https://github.com/traex/RippleEffect
RippleView的效果如下
RippleView的使用方法
(1)、继承
RippleView是继承于RelativeLayout布局。
(2)、xml布局。把RippleView嵌在想产生波纹效果的控件外层。
<com.andexert.library.RippleView
android:id="@+id/more"
android:layout_width="?android:actionBarSize"
android:layout_height="?android:actionBarSize"
android:layout_toLeftOf="@+id/more2"
android:layout_margin="5dp"
rv_centered="true">
<ImageView
android:layout_width="?android:actionBarSize"
android:layout_height="?android:actionBarSize"
android:src="@android:drawable/ic_menu_edit"
android:layout_centerInParent="true
android:padding="10dp"
android:background="@android:color/holo_blue_dark"/>
</com.andexert.library.RippleView>
(3)、监听动画结束
rippleView.setOnRippleCompleteListener(newRippleView.OnRippleCompleteListener() {
@OverridepublicvoidonComplete(RippleViewrippleView) { Log.d("Sample", "Ripple completed"); } });
(4)、一些常用到的属性和对应方法
属性
|
方法
|
rv_color:波纹
|
setRippleColor()、getRippleColor();
|
rv_type:波纹的类型,有SIMPLE、DOUBLE、RECTANGLE三种
|
setRippleType()、getRippleType()
|
rv_centered:波纹是否在中心散开
|
isCentered()、
setCentered
|
rv_alpha:波纹的透明度(1~255)
|
getRippleAlpha()、 setRippleAlpha()
|
rv_zoom:是否有拉伸动画效果
|
isZooming()、
setZooming()
|
rv_zoomScale:拉伸比例,当rv_zoom为true时才生效
|
getZoomScale()、
setZoomScale()
|
引用相关的参考资料
(1)关于android 5.0 的Material Design的学习资料:http://www.open-open.com/lib/view/open1416663769680.html
(2)RippleView的Gtihub地址为:https://github.com/traex/RippleEffect