文/err0r(簡書作者)
原文鏈接:http://www.jianshu.com/p/65d2355feb4b
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。
原文鏈接:http://www.jianshu.com/p/65d2355feb4b
著作權歸作者所有,轉載請聯系作者獲得授權,並標注“簡書作者”。
MD的特色之一就是"有意義的動畫效果",讓動畫符合物理世界的規律,而不是雜亂無章,華而不實。
點擊事件產生的波紋效果就是其中之一。
那么,先讓我們看一下什么是波紋效果:

touch_feedback.gif
可以看出,點擊變得更具有質感而不是簡單的使條目變色。
讓我們在RecyclerView上做下實驗
RecyclerView默認點擊是沒有任何效果的

touch1.gif
MD之前,我們為條目添加點擊效果是這樣的
新建drawable/touch_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/super_light_grey" android:state_pressed="true"/> <item android:drawable="@color/super_light_grey" android:state_focused="true"/> <item android:drawable="@color/white"/> </selector>
然后為點擊的條目設置android:background="@drawable/touch_bg"
在點擊條目的時候便可以看到條目變色

touch2.gif
但是我們現在想要的是波紋效果,這里要注意,波紋效果只在5.0以上的設備生效
所以我們需要新建drawable-v21/touch_bg.xml
<?xml version="1.0" encoding="utf-8"?> <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@color/line_grey"> <item android:drawable="@color/white"/> </ripple>
之后,便可以看到期待的波紋效果。

touch3.gif
代碼以及第一張圖片均參考項目Animate
希望能夠對你有所幫助