【Android】炫酷的MaterialDesign Ripple 水波纹触摸动画

炫酷的MaterialDesign Ripple 水波纹触摸动画

google在android 5.0上加入了触摸反馈动画(Ripple),设置成功后,点击控件的时候会从点击位置产生一圈水波纹的扩散效果java

  • 使用系统自带资源在XML文件上配置Ripple动画

    //波纹有边界
    android:background="?android:attr/selectableItemBackground" 
    
    //波纹能够扩散出边界
    android:background="?android:attr/selectableItemBackgroundBorderless"
  • 自定义drawable配置Ripple动画

    1. 自定义drawable

      <?xml version="1.0" encoding="utf-8"?>
      <ripple xmlns:tools="http://schemas.android.com/tools" android:color="@color/red" //默认的背景颜色 xmlns:android="http://schemas.android.com/apk/res/android" tools:targetApi="lollipop"> 
       <item  android:drawable="@color/blue" /> //按下时的水波纹颜色
      </ripple>

    2. 在代码上配置动画资源

      if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
      btnTest.setBackgroundResource(R.drawable.ripple);
      }

      ITDogFire –弓长剑鸣android