Android笔记本

android波纹点击效果原理

2019-02-28  本文已影响33人  xinguoshui

该效果通常以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" />

ripple类型的xml会创建RippleDrawable

    private Drawable inflateFromTag(@NonNull String name) {
        switch (name) {
            case "selector":
                return new StateListDrawable();
            case "animated-selector":
                return new AnimatedStateListDrawable();
            case "level-list":
                return new LevelListDrawable();
            case "layer-list":
                return new LayerDrawable();
            case "transition":
                return new TransitionDrawable();
            case "ripple":
                return new RippleDrawable();
            case "adaptive-icon":
                return new AdaptiveIconDrawable();
            case "color":
                return new ColorDrawable();
            case "shape":
                return new GradientDrawable();
            case "vector":
                return new VectorDrawable();
            case "animated-vector":
                return new AnimatedVectorDrawable();
            case "scale":
                return new ScaleDrawable();
            case "clip":
                return new ClipDrawable();
            case "rotate":
                return new RotateDrawable();
            case "animated-rotate":
                return new AnimatedRotateDrawable();
            case "animation-list":
                return new AnimationDrawable();
            case "inset":
                return new InsetDrawable();
            case "bitmap":
                return new BitmapDrawable();
            case "nine-patch":
                return new NinePatchDrawable();
            default:
                return null;
        }
    }

作者:七号大蒜
链接:https://www.jianshu.com/p/665c9d85c21d
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

上一篇下一篇

猜你喜欢

热点阅读