• Android原生实现控件Ripple方案(API28及以上)


    Android控件的水波纹效果的实现方式有很多种,比如使用ripple文件,这里介绍一下另一种Android原生的水波纹实现方案(API28及以上)。

    我们利用RippleDrawable来实现一个带Ripple的Button。RippleDrawable可以通过xml 中定义 ripple来实现,或者通过代码中动态创建RippleDrawable设置给控件。
    
    • 1

    自定义属性

    添加ripple相关的自定义属性

     
    		
            
            
            
            ...
    
    
     
            
            
        
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    RippleView接口

    创建RippleView通用接口

    public interface RippleView {
        /**
         * Gets the ripple drawable.
         *
         * @return the ripple drawable. Can be null.
         */
        RippleDrawable getRippleDrawable();
    
        /**
         * Sets the ripple drawable. This method doesn't break the background.
         *
         * @param rippleDrawable the ripple drawable. Can be null
         */
        void setRippleDrawable(RippleDrawable rippleDrawable);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    RippleView接口中的RippleDrawable也是一个接口,里面管理了RippleDrawable文件对象的创建。

    RippleDrawable接口

    public interface RippleDrawable{
    
        Drawable getBackground();
    
        enum Style {
            Background, Borderless
        }
    
        boolean setState(int[] stateSet);
    
        void draw(Canvas canvas);
        Style getStyle();
    
        boolean isHotspotEnabled();
    
        void setHotspotEnabled(boolean useHotspot);
    
        void setBounds(int left, int top, int right, int bottom);
    
        void setBounds(Rect bounds);
    
        void setHotspot(float x, float y);
    
        boolean isStateful();
    
        void setCallback(Drawable.Callback cb);
    
        ColorStateList getColor();
    
        void setRadius(int radius);
    
        int getRadius();
    
        static RippleDrawable create(ColorStateList color, Style style, View view, boolean useHotspot, int radius) {
            RippleDrawable rippleDrawable = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                rippleDrawable = new RippleDrawableMarshmallow(color, style == Style.Background ? view.getBackground() : null, style);
            } else if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
                rippleDrawable = new RippleDrawableLollipop(color, style == Style.Background ? view.getBackground() : null, style);
            }
            rippleDrawable.setCallback(view);
            rippleDrawable.setHotspotEnabled(useHotspot);
            rippleDrawable.setRadius(radius);
            return rippleDrawable;
        }
    
        static RippleDrawable create(ColorStateList color, Style style, View view, Drawable background, boolean useHotspot, int radius) {
            RippleDrawable rippleDrawable = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                rippleDrawable = new RippleDrawableMarshmallow(color, background, style);
            } else if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
                rippleDrawable = new RippleDrawableLollipop(color, background, style);
            }
            rippleDrawable.setCallback(view);
            rippleDrawable.setHotspotEnabled(useHotspot);
            rippleDrawable.setRadius(radius);
            return rippleDrawable;
        }
    }
    
    
    • 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
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    通过提供出去的create方法可以生成一个RippleDrawable文件对象,根据版本号生成不同的RippleDrawableMarshmallow对象 或 RippleDrawableLollipop对象。

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public class RippleDrawableMarshmallow extends android.graphics.drawable.RippleDrawable implements RippleDrawable {
    
        private final ColorStateList color;
        private final Drawable background;
        private Style style;
        private boolean useHotspot;
    
        public RippleDrawableMarshmallow(ColorStateList color, Drawable background, Style style) {
            super(color, background, style == Style.Borderless ? null : new ColorDrawable(0xffffffff));
            this.style = style;
            this.color = color;
            this.background = background;
        }
    
        @Override
        public Drawable getBackground() {
            return background;
        }
    
        @Override
        public Style getStyle() {
            return style;
        }
    
        @Override
        public boolean isHotspotEnabled() {
            return useHotspot;
        }
    
        @Override
        public void setHotspotEnabled(boolean useHotspot) {
            this.useHotspot = useHotspot;
        }
    
        @Override
        public ColorStateList getColor() {
            return color;
        }
    }
    
    
    • 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
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public class RippleDrawableLollipop extends android.graphics.drawable.RippleDrawable implements RippleDrawable {
    
        private final ColorStateList color;
        private final Drawable background;
        private Style style;
        private boolean useHotspot;
        private int radius;
    
        public RippleDrawableLollipop(ColorStateList color, Drawable background, Style style) {
            super(color, background, style == Style.Borderless ? null : new ColorDrawable(0xffffffff));
            this.style = style;
            this.color = color;
            this.background = background;
        }
    
        @Override
        public Drawable getBackground() {
            return background;
        }
    
        @Override
        public Style getStyle() {
            return style;
        }
    
        @Override
        public boolean isHotspotEnabled() {
            return useHotspot;
        }
    
        @Override
        public void setHotspotEnabled(boolean useHotspot) {
            this.useHotspot = useHotspot;
        }
    
        @Override
        public ColorStateList getColor() {
            return color;
        }
    
        @Override
        public void setRadius(int radius) {
            this.radius = radius;
            try {
                Method setMaxRadiusMethod = android.graphics.drawable.RippleDrawable.class.getDeclaredMethod("setMaxRadius", int.class);
                setMaxRadiusMethod.invoke(this, radius);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public int getRadius() {
            return radius;
        }
    }
    
    
    • 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
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    ShapeButton

    ShapeButton 实现RippleView接口:

    public class ShapeButton extends AppCompatButton
            implements ShadowView,
            ShapeModelView,
            RippleView {
       
    
      ...
    
        private static int[] rippleIds = new int[]{
                R.styleable.shape_button_carbon_rippleColor,
                R.styleable.shape_button_carbon_rippleStyle,
                R.styleable.shape_button_carbon_rippleHotspot,
                R.styleable.shape_button_carbon_rippleRadius
        };
    
        private void initButton(AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.shape_button, defStyleAttr, defStyleRes);
            Carbon.initElevation(this, a, elevationIds);
            Carbon.initCornerCutRadius(this,a,cornerCutRadiusIds);
            // 初始化ripple相关属性
            Carbon.initRippleDrawable(this,a,rippleIds);
            a.recycle();
        }
    
    
    
        // -------------------------------
        // shadow
        // -------------------------------
        
       ... ... ...
    
        // -------------------------------
        // shape
        // -------------------------------
       
       ... ... ...
    
        // -------------------------------
        // ripple
        // -------------------------------
    
        private RippleDrawable rippleDrawable;
    
        @Override
        public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
    		// 为rippledrawable设置扩散点
            if (rippleDrawable != null && event.getAction() == MotionEvent.ACTION_DOWN)
                rippleDrawable.setHotspot(event.getX(),event.getY());
    
            return super.dispatchTouchEvent(event);
        }
    
        @Override
        public RippleDrawable getRippleDrawable() {
            return rippleDrawable;
        }
    
        @Override
        public void setRippleDrawable(RippleDrawable newRipple) {
           // todo -- 为控件设置RippleDrawable作为背景
        }
    
    }
    
    • 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
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64

    初始化ripple相关属性:

        public static void initRippleDrawable(RippleView rippleView, TypedArray a, int[] ids) {
            int carbon_rippleColor = ids[0];
            int carbon_rippleStyle = ids[1];
            int carbon_rippleHotspot = ids[2];
            int carbon_rippleRadius = ids[3];
    
            View view = (View) rippleView;
            if (view.isInEditMode())
                return;
    
            ColorStateList color = a.getColorStateList(carbon_rippleColor);
    
            if (color != null) {
                RippleDrawable.Style style = RippleDrawable.Style.values()[a.getInt(carbon_rippleStyle, RippleDrawable.Style.Background.ordinal())];
                boolean useHotspot = a.getBoolean(carbon_rippleHotspot, true);
                int radius = (int) a.getDimension(carbon_rippleRadius, -1);
    
                rippleView.setRippleDrawable(RippleDrawable.create(color, style, view, useHotspot, radius));
            }
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    Background

    我们首先实现Style为background的情况:

        @Override
        public void setRippleDrawable(RippleDrawable newRipple) {
            if (newRipple != null) {
                newRipple.setCallback(this);
                newRipple.setBounds(0, 0, getWidth(), getHeight());
                newRipple.setState(getDrawableState());
                ((Drawable) newRipple).setVisible(getVisibility() == VISIBLE, false);
                // 其实就只是为控件设置了一个background,这个background是一个rippleDrawable图片
                if (newRipple.getStyle() == RippleDrawable.Style.Background)
                    super.setBackgroundDrawable((Drawable) newRipple);
            }
    
            rippleDrawable = newRipple;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    如何使用:

                
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    Borderless

    要实现borderless效果,我们首先需要为rippleDrawable添加状态,例如按压状态state_press

        @Override
        protected void drawableStateChanged() {
            super.drawableStateChanged();
            if (rippleDrawable != null && rippleDrawable.getStyle() != RippleDrawable.Style.Background)
                rippleDrawable.setState(getDrawableState());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    其次,borderless要求超出控件边界,其实是把子控件的rippledrawable显示在了父控件上,因此在刷新子控件背景时需要刷新父控件:

     @Override
        public void invalidateDrawable(@NonNull Drawable drawable) {
            super.invalidateDrawable(drawable);
            invalidateParentIfNeeded();
        }
    
        @Override
        public void invalidate(@NonNull Rect dirty) {
            super.invalidate(dirty);
            invalidateParentIfNeeded();
        }
    
        @Override
        public void invalidate(int l, int t, int r, int b) {
            super.invalidate(l, t, r, b);
            invalidateParentIfNeeded();
        }
    
        @Override
        public void invalidate() {
            super.invalidate();
            invalidateParentIfNeeded();
        }
    
        private void invalidateParentIfNeeded() {
            if (getParent() == null || !(getParent() instanceof View))
                return;
    
            if (rippleDrawable != null && rippleDrawable.getStyle() == RippleDrawable.Style.Borderless)
                ((View) getParent()).invalidate();
        }
    
    • 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

    假如当前Button的父控件是LinearLayout,则需要我们重写LinearLayout的drawChild方法来显示子控件的rippleDrawable:

    public class LinearLayout extends LinearLayoutCompat {
        public LinearLayout(@NonNull Context context) {
            super(context);
        }
    
        public LinearLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }
    
        public LinearLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
            if (child instanceof RippleView) {
                RippleView rippleView = (RippleView) child;
                RippleDrawable rippleDrawable = rippleView.getRippleDrawable();
                if (rippleDrawable != null && rippleDrawable.getStyle() == RippleDrawable.Style.Borderless) {
                    int saveCount = canvas.save();
                    canvas.translate(child.getLeft() + child.getWidth()/2f, child.getTop() + child.getHeight()/2f);
                    canvas.concat(child.getMatrix());
                    // 在子控件中间显示子控件的rippledrawable
                    rippleDrawable.draw(canvas);
                    canvas.restoreToCount(saveCount);
                }
            }
    
            return super.drawChild(canvas, child, drawingTime);
        }
    }
    
    • 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

    如何使用:

    
                
                
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    完整代码:

    public class ShapeButton extends AppCompatButton
            implements ShadowView,
            ShapeModelView,
            RippleView {
        public ShapeButton(@NonNull Context context) {
            super(context);
            initButton(null, android.R.attr.buttonStyle, R.style.carbon_Button);
        }
    
        public ShapeButton(@NonNull Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            initButton(attrs, android.R.attr.buttonStyle, R.style.carbon_Button);
        }
    
        public ShapeButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            initButton(attrs, defStyleAttr, R.style.carbon_Button);
        }
    
        public ShapeButton(Context context, String text, OnClickListener listener) {
            super(context);
            initButton(null, android.R.attr.buttonStyle, R.style.carbon_Button);
            setText(text);
            setOnClickListener(listener);
        }
    
        private static int[] elevationIds = new int[]{
                R.styleable.shape_button_carbon_elevation,
                R.styleable.shape_button_carbon_elevationShadowColor,
                R.styleable.shape_button_carbon_elevationAmbientShadowColor,
                R.styleable.shape_button_carbon_elevationSpotShadowColor
        };
    
        private static int[] cornerCutRadiusIds = new int[]{
                R.styleable.shape_button_carbon_cornerRadiusTopStart,
                R.styleable.shape_button_carbon_cornerRadiusTopEnd,
                R.styleable.shape_button_carbon_cornerRadiusBottomStart,
                R.styleable.shape_button_carbon_cornerRadiusBottomEnd,
                R.styleable.shape_button_carbon_cornerRadius,
                R.styleable.shape_button_carbon_cornerCutTopStart,
                R.styleable.shape_button_carbon_cornerCutTopEnd,
                R.styleable.shape_button_carbon_cornerCutBottomStart,
                R.styleable.shape_button_carbon_cornerCutBottomEnd,
                R.styleable.shape_button_carbon_cornerCut
        };
    
        private static int[] rippleIds = new int[]{
                R.styleable.shape_button_carbon_rippleColor,
                R.styleable.shape_button_carbon_rippleStyle,
                R.styleable.shape_button_carbon_rippleHotspot,
                R.styleable.shape_button_carbon_rippleRadius
        };
    
        protected TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
    
        private void initButton(AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.shape_button, defStyleAttr, defStyleRes);
            Carbon.initElevation(this, a, elevationIds);
            Carbon.initCornerCutRadius(this,a,cornerCutRadiusIds);
            Carbon.initRippleDrawable(this,a,rippleIds);
            a.recycle();
        }
    
    
    
        // -------------------------------
        // shadow
        // -------------------------------
        private float elevation = 0;
        private float translationZ = 0;
        private ColorStateList ambientShadowColor, spotShadowColor;
        @Override
        public float getElevation() {
            return elevation;
        }
    
        @Override
        public void setElevation(float elevation) {
            if (Carbon.IS_PIE_OR_HIGHER) {
                super.setElevation(elevation);
                super.setTranslationZ(translationZ);
            } else if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
                if (ambientShadowColor == null || spotShadowColor == null) {
                    super.setElevation(elevation);
                    super.setTranslationZ(translationZ);
                } else {
                    super.setElevation(0);
                    super.setTranslationZ(0);
                }
            } else if (elevation != this.elevation && getParent() != null) {
                ((View) getParent()).postInvalidate();
            }
            this.elevation = elevation;
        }
    
        @Override
        public float getTranslationZ() {
            return translationZ;
        }
    
        public void setTranslationZ(float translationZ) {
            if (translationZ == this.translationZ)
                return;
            if (Carbon.IS_PIE_OR_HIGHER) {
                super.setTranslationZ(translationZ);
            } else if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
                if (ambientShadowColor == null || spotShadowColor == null) {
                    super.setTranslationZ(translationZ);
                } else {
                    super.setTranslationZ(0);
                }
            } else if (translationZ != this.translationZ && getParent() != null) {
                ((View) getParent()).postInvalidate();
            }
            this.translationZ = translationZ;
        }
    
        @Override
        public ColorStateList getElevationShadowColor() {
            return ambientShadowColor;
        }
    
        @Override
        public void setElevationShadowColor(ColorStateList shadowColor) {
            ambientShadowColor = spotShadowColor = shadowColor;
            setElevation(elevation);
            setTranslationZ(translationZ);
        }
    
        @Override
        public void setElevationShadowColor(int color) {
            ambientShadowColor = spotShadowColor = ColorStateList.valueOf(color);
            setElevation(elevation);
            setTranslationZ(translationZ);
        }
    
        @Override
        public void setOutlineAmbientShadowColor(ColorStateList color) {
            ambientShadowColor = color;
            if (Carbon.IS_PIE_OR_HIGHER) {
                super.setOutlineAmbientShadowColor(color.getColorForState(getDrawableState(), color.getDefaultColor()));
            } else {
                setElevation(elevation);
                setTranslationZ(translationZ);
            }
        }
    
        @Override
        public void setOutlineAmbientShadowColor(int color) {
            setOutlineAmbientShadowColor(ColorStateList.valueOf(color));
        }
    
        @Override
        public int getOutlineAmbientShadowColor() {
            return ambientShadowColor.getDefaultColor();
        }
    
        @Override
        public void setOutlineSpotShadowColor(int color) {
            setOutlineSpotShadowColor(ColorStateList.valueOf(color));
        }
    
        @Override
        public void setOutlineSpotShadowColor(ColorStateList color) {
            spotShadowColor = color;
            if (Carbon.IS_PIE_OR_HIGHER) {
                super.setOutlineSpotShadowColor(color.getColorForState(getDrawableState(), color.getDefaultColor()));
            } else {
                setElevation(elevation);
                setTranslationZ(translationZ);
            }
    
        }
    
        @Override
        public int getOutlineSpotShadowColor() {
            return ambientShadowColor.getDefaultColor();
        }
    
        @Override
        public boolean hasShadow() {
            return false;
        }
    
        @Override
        public void drawShadow(Canvas canvas) {
    
        }
    
        @Override
        public void draw(Canvas canvas) {
            boolean c = !Carbon.isShapeRect(shapeModel, boundsRect);
    
            if (Carbon.IS_PIE_OR_HIGHER) {
                if (spotShadowColor != null)
                    super.setOutlineSpotShadowColor(spotShadowColor.getColorForState(getDrawableState(), spotShadowColor.getDefaultColor()));
                if (ambientShadowColor != null)
                    super.setOutlineAmbientShadowColor(ambientShadowColor.getColorForState(getDrawableState(), ambientShadowColor.getDefaultColor()));
            }
    
            // 判断如果不是圆角矩形,需要使用轮廓Path,绘制一下Path,不然显示会很奇怪
            if (getWidth() > 0 && getHeight() > 0 && ((c && !Carbon.IS_LOLLIPOP_OR_HIGHER) || !shapeModel.isRoundRect(boundsRect))) {
                int saveCount = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
                super.draw(canvas);
                paint.setXfermode(Carbon.CLEAR_MODE);
                if (c) {
                    cornersMask.setFillType(Path.FillType.INVERSE_WINDING);
                    canvas.drawPath(cornersMask, paint);
                }
                canvas.restoreToCount(saveCount);
                paint.setXfermode(null);
            }else{
                super.draw(canvas);
            }
        }
    
        // -------------------------------
        // shape
        // -------------------------------
        private ShapeAppearanceModel shapeModel = new ShapeAppearanceModel();
        private MaterialShapeDrawable shadowDrawable = new MaterialShapeDrawable(shapeModel);
        @Override
        public void setShapeModel(ShapeAppearanceModel shapeModel) {
            this.shapeModel = shapeModel;
            shadowDrawable = new MaterialShapeDrawable(shapeModel);
            if (getWidth() > 0 && getHeight() > 0)
                updateCorners();
            if (!Carbon.IS_LOLLIPOP_OR_HIGHER)
                postInvalidate();
        }
        // View的轮廓形状
        private RectF boundsRect = new RectF();
        // View的轮廓形状形成的Path路径
        private Path cornersMask = new Path();
    
        /**
         * 更新圆角
         */
        private void updateCorners() {
            if (Carbon.IS_LOLLIPOP_OR_HIGHER) {
                // 如果不是矩形,裁剪View的轮廓
                if (!Carbon.isShapeRect(shapeModel, boundsRect)){
                    setClipToOutline(true);
                }
                //该方法返回一个Outline对象,它描述了该视图的形状。
                setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        if (Carbon.isShapeRect(shapeModel, boundsRect)) {
                            outline.setRect(0, 0, getWidth(), getHeight());
                        } else {
                            shadowDrawable.setBounds(0, 0, getWidth(), getHeight());
                            shadowDrawable.setShadowCompatibilityMode(MaterialShapeDrawable.SHADOW_COMPAT_MODE_NEVER);
                            shadowDrawable.getOutline(outline);
                        }
                    }
                });
            }
            // 拿到圆角矩形的形状
            boundsRect.set(shadowDrawable.getBounds());
            // 拿到圆角矩形的Path
            shadowDrawable.getPathForSize(getWidth(), getHeight(), cornersMask);
        }
    
        @Override
        public ShapeAppearanceModel getShapeModel() {
            return this.shapeModel;
        }
    
        @Override
        public void setCornerCut(float cornerCut) {
            shapeModel = ShapeAppearanceModel.builder().setAllCorners(new CutCornerTreatment(cornerCut)).build();
            setShapeModel(shapeModel);
        }
    
        @Override
        public void setCornerRadius(float cornerRadius) {
            shapeModel = ShapeAppearanceModel.builder().setAllCorners(new RoundedCornerTreatment(cornerRadius)).build();
            setShapeModel(shapeModel);
        }
    
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
    
            if (!changed)
                return;
    
            if (getWidth() == 0 || getHeight() == 0)
                return;
    
            updateCorners();
    
        }
    
        // -------------------------------
        // ripple
        // -------------------------------
    
        private RippleDrawable rippleDrawable;
    
        @Override
        public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
    
            if (rippleDrawable != null && event.getAction() == MotionEvent.ACTION_DOWN)
                rippleDrawable.setHotspot(event.getX(),event.getY());
    
            return super.dispatchTouchEvent(event);
        }
    
        @Override
        public RippleDrawable getRippleDrawable() {
            return rippleDrawable;
        }
    
        @Override
        public void setRippleDrawable(RippleDrawable newRipple) {
            if (newRipple != null) {
                newRipple.setCallback(this);
                newRipple.setBounds(0, 0, getWidth(), getHeight());
                newRipple.setState(getDrawableState());
                ((Drawable) newRipple).setVisible(getVisibility() == VISIBLE, false);
                if (newRipple.getStyle() == RippleDrawable.Style.Background)
                    super.setBackgroundDrawable((Drawable) newRipple);
            }
    
            rippleDrawable = newRipple;
        }
    
        @Override
        protected void drawableStateChanged() {
            super.drawableStateChanged();
            if (rippleDrawable != null && rippleDrawable.getStyle() != RippleDrawable.Style.Background)
                rippleDrawable.setState(getDrawableState());
        }
    
    
        @Override
        public void invalidateDrawable(@NonNull Drawable drawable) {
            super.invalidateDrawable(drawable);
            invalidateParentIfNeeded();
        }
    
        @Override
        public void invalidate(@NonNull Rect dirty) {
            super.invalidate(dirty);
            invalidateParentIfNeeded();
        }
    
        @Override
        public void invalidate(int l, int t, int r, int b) {
            super.invalidate(l, t, r, b);
            invalidateParentIfNeeded();
        }
    
        @Override
        public void invalidate() {
            super.invalidate();
            invalidateParentIfNeeded();
        }
    
        private void invalidateParentIfNeeded() {
            if (getParent() == null || !(getParent() instanceof View))
                return;
    
            if (rippleDrawable != null && rippleDrawable.getStyle() == RippleDrawable.Style.Borderless)
                ((View) getParent()).invalidate();
        }
    
    }
    
    
    • 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
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313
    • 314
    • 315
    • 316
    • 317
    • 318
    • 319
    • 320
    • 321
    • 322
    • 323
    • 324
    • 325
    • 326
    • 327
    • 328
    • 329
    • 330
    • 331
    • 332
    • 333
    • 334
    • 335
    • 336
    • 337
    • 338
    • 339
    • 340
    • 341
    • 342
    • 343
    • 344
    • 345
    • 346
    • 347
    • 348
    • 349
    • 350
    • 351
    • 352
    • 353
    • 354
    • 355
    • 356
    • 357
    • 358
    • 359
    • 360
    • 361
    • 362
    • 363
    • 364
    • 365
    • 366
    • 367
    • 368
    • 369
    • 370
    • 371
  • 相关阅读:
    文件服务之FTP
    2024-06-12 问AI: 在大语言模型中,什么是Jailbreak漏洞?
    为何重要?解析企业实行网络安全等级保护的必要性
    Java List集合排序 Java8 List集合排序方法 Java Lambda集合排序
    面试真的被问麻了......
    聚合支付的特点与应用建议
    冥想第五百五十九天
    【王道】计算机网络应用层(五)
    【手把手带你刷好题】Java刷题记录 21——>>27
    Vue、fabricJS 画布实现自由绘制折线
  • 原文地址:https://blog.csdn.net/jxq1994/article/details/133687352