最近在看動畫的使用。
在看Android官方文檔時,看到觸摸反饋的文章,就自己動手實驗下效果,順便記錄下使用方法。
具體使用地址:https://developer.android.com/training/material/animations.html#Touch
創建drawable resource file
使用如下:
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ripple_green"
android:text="Button"/>
1、沒有邊界的ripple
no_mask_ripple.gif
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/green_virtual">
</ripple>
2、有邊界的ripple
has_mask_ripple.gif
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/green_virtual">
<item
android:id="@android:id/mask"
android:drawable="@color/black" />
</ripple>
在ripple中添加item,同時設置id為@android:id/mask,其中item的drawable屬性不知道干嘛用的,不設置的話會報錯~,我這就隨便添加了個顏色。
3、帶圖片的ripple
has_pic_ripple.gif
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/green_virtual">
<item
android:id="@android:id/mask"
android:drawable="@drawable/bmc_img_jxjj_star_1" />
</ripple>
ok,上面咱們不知道item的drawable屬性作用,現在就知道其作用了,添加了圖片之后,其水波紋就只能在圖片的非透明區域顯示了。
4、添加自定義shape的ripple
上面的drawable屬性已經嘗試過用圖片了,那用自定義的shape如何呢?
custom_shape_ripple.gif
我自定義了一個圓形的shape,里面設置的顏色還是沒什么用的。
shape的xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/black" />
</shape>
該shape放到drawable屬性中
5、添加selector的ripple
現在,我們在item中添加selector再看看效果
selector_ripple.gif
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/green_virtual">
<item>
<selector>
<item
android:drawable="@drawable/bmc_main_bottom_zy_checked"
android:state_pressed="true" />
<item
android:drawable="@drawable/bmc_main_bottom_zy_unchecked"
android:state_pressed="false" />
</selector>
</item>
</ripple>
我的圖片默認狀態是灰色,點擊后是紅色,而我在ripple中設置的color是綠色,這里就能看出水波紋這一層的效果就像是覆蓋在圖片上一樣。
至此,ripple的使用差不多結束了,如果在開發中發現新的使用方法,我再來添加~~