Android L限制Ripple水波纹范围大小
2017-03-07 10:18
190 查看
Ripple简介
Android 5.0之后google推出了Material Design,Botton默认的触摸反馈会有水波纹涟漪效果。而这种水波纹的效果实现主要依赖于RippleDrawable。以下会介绍Ripple的基本使用及关于控制水波纹范围的三种处理方法,仅作点明思路及学习笔记不作具体实现。
基本使用
该效果通常以background的形式呈现,在XML中可以引用以下两个系统自带属性:- android:background=”?android:attr/selectableItemBackground” 有边界波纹
- android:background=”?android:attr/··” 超出边界波纹。该波纹由父布局绘制及限制边界(API 21提供)
以
selectableItemBackground为例看下系统属性的实现原理,发现该属性的定义最终指向
<item name="selectableItemBackground">@drawable/item_background_material</item>,
查看该Drawable文件内容为:
<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>1
2
3
4
5
6
1
2
3
4
5
6
selectableItemBackgroundBorderless所对应Drawable内容为:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?attr/colorControlHighlight" />1
1
XML控制
特点:简单,用于固定的view的处理,但灵活性不高。目前网络上的资料偏向于如何在xml的item下做文章,如在ripple中添加shape来限制范围,验证效果反而有各种小坑(谁验谁知道)。殊不知官方早已提供解决方案。
根据官方对于RippleDrawable的说明文档,ripple的xml标签支持两个属性,分别是
color色调和
radius波纹半径。故我们在使用时只需要新建ripple并以
`android:background的形式调用即可.
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:attr/colorControlHighlight" android:radius="@dimen/ripple_radius" />1
2
3
1
2
3
自定义RippleDrawable
特点:可以动态控制,灵活性超级高,但对应的处理复杂度和难度也较高。设置水波纹点击效果的本质其实就是设置一个background,最为灵活的方法当然是自定义ripple,然后对目标View直
4000
接setBackground即可.
RippleDrawable继承于Drawable
Java.lang.Object
↳ android.graphics.drawable.Drawable
↳ android.graphics.drawable.LayerDrawable
↳ android.graphics.drawable.RippleDrawable
自定义时可以继承RippleDrawable也可以直接继承Drawable,两者的本质分别是实现
setRadius()和实现
setHotspotBounds(),殊途同归,均可以达到动态限制波纹大小的效果。系统的虚拟键NavigationBar就是使用的后者。
折中方案
特点:简单,灵活适中,易上手以
selectableItemBackgroundBorderless超出边界范围为基础,以
setHotspotBounds()的方式动态控制其波纹范围。
以下提供的是个简易工具demo,调用时传入对应的view
xxx.setBackground(RippleUtils. getRippleDrawable(context, targetView)),也可以自己定义增加一个控制ripple范围的方法:
/** * Created by vito on 16-11-1. */ public class RippleUtils { private static RippleDrawable mRipple; private static Drawable mTileBackground; private static Drawable newTileBackground(Context context) { final int[] attrs = new int[]{android.R.attr.selectableItemBackgroundBorderless}; final TypedArray ta = context.obtainStyledAttributes(attrs); final Drawable d = ta.getDrawable(0); ta.recycle(); return d; } private static void setRipple(RippleDrawable tileBackground, View v) { mRipple = tileBackground; updateRippleSize(v); } //以view的中心为圆心,宽的1/4为半径的ripple范围 private static void updateRippleSize(View v) { // center the touch feedback on the center of the icon, and dial it down a bit if (v.getWidth() != 0) { final int cx = v.getWidth() / 2; final int cy = v.getHeight() / 2; final int rad = (int) (v.getWidth() * .25f); Log.d("ripple", "updateRippleSize: rad=" + rad); mRipple.setHotspotBounds(cx - rad, cy - rad, cx + rad, cy + rad); } else { // TODO: 17-1-9 } } //对外接口 public static RippleDrawable getRippleDrawable(Context context, View view) { mTileBackground = newTileBackground(context); if (mTileBackground instanceof RippleDrawable) { setRipple((RippleDrawable) mTileBackground, view); } return mRipple; } }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
相关文章推荐
- Android L限制Ripple水波纹范围大小
- .net的session详解 存储模式 存到数据库中 使用范围与大小限制 生命周期
- Android Tip1:获取 android 每个 app 内存限制大小
- 窗口拖动的大小范围限制
- Android 常见问题之Assets文件大小限制
- .net的session详解 存储模式 存到数据库中 使用范围与大小限制 生命周期
- android应用程序中res文件夹里可放的资源有大小限制
- android:clipChildren,是否限制子View在其范围内
- Android项目技术总结:获取状态栏和标题栏并计算长宽来限制绘制浮动窗口活动范围
- Android 常见问题之Assets文件大小限制
- C#数值的大小范围限制处理.
- Android 常见问题之Assets文件大小限制
- Android项目技术总结:获取状态栏和标题栏并计算长宽来限制绘制浮动窗口活动范围
- Android项目技术总结:获取状态栏和标题栏并计算长宽来限制绘制浮动窗口活动范围
- Android EditText 对输入字数和内容范围进行限制
- .net的session详解 存储模式 存到数据库中 使用范围与大小限制 生命周期
- Android资源文件的大小限制及解决方案
- Android Assets文件大小限制
- android 设置录制视频的时长与大小限制,超时、超大小时自动停止
- 限制窗体的移动范围和缩放大小