RippleDrawable 的简单使用

http://m.blog.csdn.net/article/details?id=52451848


RippleDrawable 就是一个自定义触摸反馈动画。是在 Android 5.0 时推出的动画效果。为了兼容低版本,可以在将 <ripple> 元素定义在 drawable-v21 目录下;

效果如下:



首先定义一个 StateListDrawable 资源;

button_default.xml :

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item  
  4.         android:state_enabled="false"  
  5.         android:drawable="@drawable/disabled" />  
  6.     <item  
  7.         android:state_pressed="true"  
  8.         android:drawable="@drawable/selected" />  
  9.     <item  
  10.         android:state_focused="true"  
  11.         android:drawable="@drawable/selected" />  
  12.   
  13.     <item android:drawable="@drawable/fdefault" />  
  14. </selector>  


需要注意 <selector> 时有序的。Android在遍历烈表时,会返回第一个状态完全匹配的 Drawable 。所以将常规状态放在烈表的最底部。




使用一些动画触摸反馈修饰按钮示例。使用 <ripple> 元素定义 XML (在 drawable-v21目录下),可用于构造 RippleDrawable;

background_button.xml :

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ripple xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:color="#0CC">  
  4.     <!--显示默认的 drawable-->  
  5.     <item android:drawable="@drawable/button_default" />  
  6.   
  7.     <!--匹配默认值的波纹效果剪切遮罩-->  
  8.     <item  
  9.         android:id="@android:id/mask"  
  10.         android:drawable="@drawable/button_default" />  
  11. </ripple>  



增加尺寸的缩放效果(res/animator 目录下);

button_press.xml :

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item  
  4.         android:state_enabled="true"  
  5.         android:state_pressed="true">  
  6.         <set android:ordering="together">  
  7.             <objectAnimator  
  8.                 android:duration="@android:integer/config_shortAnimTime"  
  9.                 android:propertyName="scaleX"  
  10.                 android:valueTo="0.8"  
  11.                 android:valueType="floatType" />  
  12.             <objectAnimator  
  13.                 android:duration="@android:integer/config_shortAnimTime"  
  14.                 android:propertyName="scaleY"  
  15.                 android:valueTo="0.8"  
  16.                 android:valueType="floatType" />  
  17.         </set>  
  18.     </item>  
  19.   
  20.     <item>  
  21.         <set android:ordering="together">  
  22.             <objectAnimator  
  23.                 android:duration="@android:integer/config_shortAnimTime"  
  24.                 android:propertyName="scaleX"  
  25.                 android:valueTo="1.0"  
  26.                 android:valueType="floatType" />  
  27.             <objectAnimator  
  28.                 android:duration="@android:integer/config_shortAnimTime"  
  29.                 android:propertyName="scaleY"  
  30.                 android:valueTo="1.0"  
  31.                 android:valueType="floatType" />  
  32.         </set>  
  33.     </item>  
  34. </selector>  

虽然 XML 结构使用和前面的 button_default.xml 相同的 <seletor>,但是在 res/animator 目录下,表示动画器实例而非 Drawable 的状态列表集合。




content_main.xml :

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:paddingBottom="@dimen/activity_vertical_margin"  
  8.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  9.     android:paddingRight="@dimen/activity_horizontal_margin"  
  10.     android:paddingTop="@dimen/activity_vertical_margin"  
  11.     app:layout_behavior="@string/appbar_scrolling_view_behavior"  
  12.     tools:context="com.crazy.bitmapxfermode.MainActivity"  
  13.     tools:showIn="@layout/activity_main">  
  14.   
  15.     <Button  
  16.         android:text="My Button"  
  17.         android:background="@drawable/background_button"  
  18.         android:stateListAnimator="@animator/button_press"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content" />  
  21. </RelativeLayout>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值