美文网首页Android DevAndroid Class手机移动程序开发
Android L限制Ripple水波纹范围大小

Android L限制Ripple水波纹范围大小

作者: 七号大蒜 | 来源:发表于2017-01-09 14:53 被阅读1141次
    Meterial Design

    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>
    

    selectableItemBackgroundBorderless所对应Drawable内容为:

    <ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?attr/colorControlHighlight" />
    
    RippleDrawable

    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" />
    

    自定义RippleDrawable

    特点:可以动态控制,灵活性超级高,但对应的处理复杂度和难度也较高。

    设置水波纹点击效果的本质其实就是设置一个background,最为灵活的方法当然是自定义ripple,然后对目标View直接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,调用时传入对应的viewxxx.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;
        }
    }
    

    相关文章

      网友评论

      • 4b01863fd657:不管是官方的android:background="?android:selectableItemBackgroundBorderless"属性还是文章中给出的方案,5.0以下系统运行都会崩溃,不知道怎么解决。。。
        七号大蒜:@liuly0218 你用兼容 5.0以下要做水波纹不是这种
      • 内涵却风骚:楼下是joe:smirk:
      • 0f13a435bc1c:厉害了
        七号大蒜:@小美人小黑子 亲爱的 你忘了打赏我

      本文标题:Android L限制Ripple水波纹范围大小

      本文链接:https://www.haomeiwen.com/subject/cjchbttx.html