google在android 5.0上加入了觸摸反饋動畫(Ripple),設置成功后,點擊控件的時候會從點擊位置產生一圈水波紋的擴散效果
-
使用系統自帶資源在XML文件上配置Ripple動畫
//波紋有邊界 android:background="?android:attr/selectableItemBackground" //波紋可以擴散出邊界 android:background="?android:attr/selectableItemBackgroundBorderless"
-
自定義drawable配置Ripple動畫
-
自定義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>
?
-
在代碼上配置動畫資源
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { btnTest.setBackgroundResource(R.drawable.ripple); }
-
-
相關文檔
- AndroidDeveloper:
https://developer.android.com/reference/android/graphics/drawable/RippleDrawable.html
?
- AndroidDeveloper: