在android5.0新增了ripple的效果,他可以运用到Button,ImageButton,TextView,以及一些布局容器。
首先,我们看android:Theme.Material.Light主题中设置的各种ripple效果属性的默认值:
1.<item name="selectableItemBackground">@drawable/item_background_material</item> 2.<item name="selectableItemBackgroundBorderless">@drawable/item_background_borderless_material</item> 3.<item name="borderlessButtonStyle">@style/Widget.Material.Light.Button.Borderless</item> 4.<item name="buttonBarButtonStyle">@style/Widget.Material.Light.Button.ButtonBar.AlertDialog</item>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?attr/colorControlHighlight"> <item android:id="@id/mask"> <color android:color="@color/white" /> </item> </ripple>selectableItemBackgroundBorderless在drawable文件中文件内容为:无边界的效果(需要api21以上)
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?attr/colorControlHighlight" />
borderlessButtonStyle最后引用的style就是如下:
<!-- Borderless ink button --> <style name="Widget.Material.Button.Borderless"> <item name="background">@drawable/btn_borderless_material</item> <item name="stateListAnimator">@null</item> </style>他实际上就是设置了background的属性:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?attr/colorControlHighlight"> <item android:id="@id/mask" android:drawable="@drawable/btn_default_mtrl_shape" /> </ripple>
buttonBarButtonStyle实际上也是调用了borderlessButtonStyle的style,就是也是调用了相同的background:
<!-- Borderless ink button --> <style name="Widget.Material.Button.Borderless"> <item name="background">@drawable/btn_borderless_material</item> <item name="stateListAnimator">@null</item> </style> <!-- Colored borderless ink button --> <style name="Widget.Material.Button.Borderless.Colored"> <item name="textColor">?attr/colorAccent</item> <item name="stateListAnimator">@anim/disabled_anim_material</item> </style>
android:background="?android:selectableItemBackground"
style="?android:buttonBarButtonStyle"