您的位置:首页 > 其它

rippleDrawable 触摸反馈

2017-06-11 19:47 127 查看
在5.0以上设备上实现波纹效果,可以通过给View设置background实现。官方已经有两个已经实现的效果供我们选择:
1.?android:attr/selectableItemBackground
在5.0以下的设备上没有波纹效果,是普通的变色效果。波纹效果会被限制在View的大小之内。
2.?android:attr/selectableItemBackgroundBorderless。
只支持5.0及以上设备,RequiresApi = 21,波纹扩散范围是圆形的,而且圆的大小是以View最远距离点距离View中心点距离为半径,就是说:波纹的扩散范围保证会覆盖整个View,而且会扩散出View。
RippleDrawable
RippleDrawable 继承自 LayerDrawable, 可以实现多个Layer的层叠。用xml的形式来配置Ripple,就是普通的在drawable文件夹下新建一个drawable,它的最外层标签是ripple:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color=“@color/colorPrimary">

</ripple>

<Button
android:background="@drawable/ripple"
android:onClick="ripple"
android:layout_width="300dp"
android:layout_height="30dp"
android:gravity="center"
android:text=“RippleDrawable(触摸反馈)"/>

下面的实例中公用一个Button展示效果,可以看到设置了background="@drawable/ripple"
View背景成了空白。
上面这段简单的代码就帮我们实现了一个color自定义的?android:attr/selectableItemBackgroundBorderless 的效果,直接给目标view设置background属性,就能看到熟悉的波纹效果。
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimary">
<item
android:id="@android:id/mask"
android:drawable="@color/colorAccent"/>
</ripple>

在原来的代码中加上了一个item标签,还给这个标签设置了一个id=“@android:id/mask”,后面的那个drawable属性设置了波纹效果的扩散范围,这里的颜色不起作用,这时候再运行我们的程序,发现波纹扩散的范围被限制在View内部,效果等同于?android:attr/selectableItemBackground。

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/colorPrimary">

<item
android:id="@android:id/mask"
android:drawable="@color/colorAccent"/>
<item android:drawable="@color/colorAccent"/>
</ripple>

Button本身有了颜色。
分析以上代码:
1.定义在ripple节点的颜色定义的是最后我们想要的波纹的颜色。
2.里面的两个item 标签分别定义了波纹扩散的范围以及普通状态目标View的颜色,通过id设置,第一个item设置了android:id=“@android:id/mask”,所以这个item不会出现被绘制在屏幕上,只会已蒙层的形式定义波纹的范围。
3.如果ripple中有多个普通的item标签,效果会和layer-list效果类似,多个item的drawable会叠加在一起,View的背景色显示最后一个drawable。定义了maskid的item和普通item之间不会有影响。
注意:在ImageView上使用,需要设置clickable属性为true的时候,波纹效果才会生效。

colorControlHighlight

修改默认的波纹颜色,还可以直接在application的theme主题中通过colorControlHighlight来设置,使用也比较简单。

<!-- Base application theme. -->
<style name="AppTheme" parent=“Theme.AppCompat.Light.DarkActionBar">
……
<item name="colorControlHighlight">@color/colorAccent</item>
<item name="android:textAllCaps">false</item>
</style>


5.0以下的兼容方案
1. layout-v21方案,相信大家都比较熟悉,写两份ripple_drawable文件,分别在layout-v21文件夹下和普通layout文件夹下,普通layout文件夹的ripple_drawable为空或者设置普通的点击效果,只要不使用ripple标签就好。
普通:
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:drawable="@color/colorPrimary"
android:state_enabled="false"/>
<item android:drawable="@color/white"
android:state_pressed="true"/>

</selector>

2.代码设置,不太推荐。
Button mBotton = (Button) findViewById(R.id.btn_ripple);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[] arrts = new int[]{android.R.attr.selectableItemBackground};
TypedArray array = obtainStyledAttributes(arrts);
mBotton.setBackground(array.getDrawable(0));
array.recycle();
}else {
mBotton.setBackgroundResource(R.drawable.ripple);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息