Android自定义Drawable,增加渐变阴影、部分渐变(圆角、部分圆角、圆形)

自定义Drawable,增加渐变阴影、部分渐变(圆角、部分圆角、圆形)


一、先上效果图

效果图

二、思路

 1、圆角
      1)绘制shadow
    * 2)绘制遮挡部分shadow
      3)绘制圆角图
 2、部分圆角
      1)绘制shadow
    * 2)绘制遮挡部分shadow
      3)绘制圆角图
      4)绘制遮挡部分圆角
 3、圆形
      1)绘制shadow
    * 2)绘制遮挡部分shadow
      3)绘制圆形图

四、实现

1)设置paint

         /**
         * 阴影paint
         */
        mShadowPaint = new Paint();
        mShadowPaint.setAntiAlias(true);
        mShadowPaint.setShadowLayer(shadowRadius, 0, 0, shadowColor);
        /**
         * 覆盖部分背景的paint
         */
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        /**
         * 背景paint
         */
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        /**
         * 覆盖部分圆角的paint
         */
        mBgPartPaint = new Paint();
        mBgPartPaint.setAntiAlias(true);
图像合成效果示意图
     设置 覆盖部分背景的paint  DST_OUT  显示不重叠的部分

2)根据类型设置RectF

   ***** 根据类型设置RectF ******

 @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        switch (type) {
            case All:
                left = left + mShadowRadius;
                top = top + mShadowRadius;
                right = right - mShadowRadius;
                bottom = bottom - mShadowRadius;
                break;
            case TOP:
            case BOTTOM:
                top = top + mShadowRadius;
                bottom = bottom - mShadowRadius;
                break;
            case LEFT:
            case RIGHT:
                left = left + mShadowRadius;
                right = right - mShadowRadius;
                break;
            default:
                break;
        }
        mRect = new RectF(left, top, right, bottom);
    }

3)根据类型设置覆盖阴影/圆角 的 RectF

        /**
         * 设置覆盖阴影/圆角 的 RectF
         */
        RectF rect = new RectF();
        RectF rect1 = new RectF();
        switch (type) {
            case All:
                rect.left = 0;
                rect.right = 0;
                rect.top = 0;
                rect.bottom = 0;
                break;
            case TOP:
                rect.left = mRect.left;
                rect.right = mRect.right;
                rect.top = mRect.height() / 2;
                rect.bottom = mRect.bottom + mShadowRadius;
                rect1.left = mRect.left;
                rect1.right = mRect.right;
                rect1.top = mRect.height() / 2;
                rect1.bottom = mRect.bottom;
                break;
            case BOTTOM:
                rect.left = mRect.left;
                rect.right = mRect.right;
                rect.top = mRect.top - mShadowRadius;
                rect.bottom = mRect.height() / 2;

                rect1.left = mRect.left;
                rect1.right = mRect.right;
                rect1.top = mRect.top;
                rect1.bottom = mRect.height() / 2;

                break;
            case LEFT:
                rect.left = mRect.width() / 2;
                rect.right = mRect.right + mShadowRadius;
                rect.top = mRect.top;
                rect.bottom = mRect.bottom;

                rect1.left = mRect.width() / 2;
                rect1.right = mRect.right ;
                rect1.top = mRect.top;
                rect1.bottom = mRect.bottom;

                break;
            case RIGHT:
                rect.left = mRect.left-mShadowRadius;
                rect.right = mRect.width() / 2;
                rect.top = mRect.top;
                rect.bottom = mRect.bottom;

                rect1.left = mRect.left;
                rect1.right = mRect.width() / 2;
                rect1.top = mRect.top;
                rect1.bottom = mRect.bottom;
                break;

            default:
                break;
        }

4)根据 类型绘制 背景

     if (mShape == SHAPE_ROUND) {
           /**
            * 绘制全圆角阴影
            */
           canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mShadowPaint);//绘制阴影
           canvas.drawRect(rect, mPaint);//绘制不需显示阴影部分的遮盖
           canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mBgPaint);//绘制中心的图
       } else if (mShape == SHAPE_ROUND_PART) {
           /**
            * 绘制部分圆角阴影
            */
           canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mShadowPaint);//绘制阴影
           canvas.drawRect(rect, mPaint);//绘制不需显示阴影部分的遮盖
           canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mBgPaint);//绘制中心的图
           canvas.drawRect(rect1, mBgPartPaint);//绘制不需显示圆角的部分
       } else {
           /**
            * 绘制圆形阴影
            */
           canvas.drawCircle(mRect.centerX(), mRect.centerY(), Math.min(mRect.width(), mRect.height()) / 2, mShadowPaint);//绘制带阴影的圆形
           canvas.drawRect(rect, mPaint);//绘制不需显示阴影部分的遮盖
           canvas.drawCircle(mRect.centerX(), mRect.centerY(), Math.min(mRect.width(), mRect.height()) / 2, mBgPaint);//绘制中心圆
       }

4)使用(就是这么简单)

       ShadowDrawableUtil.setShadowDrawable(round_tv, ShadowDrawableUtil.TypeEnum.LEFT);
       ShadowDrawableUtil.setShadowDrawableTop(round_part_tv,"#A6000000");
       ShadowDrawableUtil.setShadowDrawableCircle(circle_tv);

6)RoundImageView完整代码


/**
 * view 添加阴影
 */
public class ShadowDrawableUtil extends Drawable {

    private Paint mShadowPaint;
    private Paint mBgPaint;
    private Paint mPaint;
    private Paint mBgPartPaint;
    private int mShadowRadius;
    private int mShape;
    private int mShapeRadius;
    private int mBgColor[];
    private RectF mRect;
    private TypeEnum type;
    private static int SHAPE_ROUND = 1;
    private static int SHAPE_CIRCLE = 2;
    private static int SHAPE_ROUND_PART = 3;

    public enum TypeEnum {
        All,
        LEFT,
        TOP,
        RIGHT,
        BOTTOM
    }


    private ShadowDrawableUtil(int shape, TypeEnum type, int[] bgColor, int shapeRadius, int shadowColor, int shadowRadius) {
        this.mShape = shape;
        this.type = type;
        this.mBgColor = bgColor;
        this.mShapeRadius = shapeRadius;
        this.mShadowRadius = shadowRadius;
        /**
         * 阴影paint
         */
        mShadowPaint = new Paint();
        mShadowPaint.setAntiAlias(true);
        mShadowPaint.setShadowLayer(shadowRadius, 0, 0, shadowColor);
        /**
         * 覆盖部分背景的paint
         */
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
        /**
         * 背景paint
         */
        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        /**
         * 覆盖部分圆角的paint
         */
        mBgPartPaint = new Paint();
        mBgPartPaint.setAntiAlias(true);
    }

    @Override
    public void setBounds(int left, int top, int right, int bottom) {
        super.setBounds(left, top, right, bottom);
        switch (type) {
            case All:
                left = left + mShadowRadius;
                top = top + mShadowRadius;
                right = right - mShadowRadius;
                bottom = bottom - mShadowRadius;
                break;
            case TOP:
            case BOTTOM:
                top = top + mShadowRadius;
                bottom = bottom - mShadowRadius;
                break;
            case LEFT:
            case RIGHT:
                left = left + mShadowRadius;
                right = right - mShadowRadius;
                break;
            default:
                break;
        }


        mRect = new RectF(left, top, right, bottom);
    }

    @Override
    public void draw(@NonNull Canvas canvas) {
        if (mBgColor != null) {
            if (mBgColor.length == 1) {
                mBgPaint.setColor(mBgColor[0]);
                mShadowPaint.setColor(mBgColor[0]);
                mBgPartPaint.setColor(mBgColor[0]);
            } else {
                mBgPaint.setShader(new LinearGradient(mRect.left, mRect.height() / 2, mRect.right,
                        mRect.height() / 2, mBgColor, null, Shader.TileMode.CLAMP));
            }
        }
        /**
         * 设置覆盖阴影/圆角 的 RectF
         */
        RectF rect = new RectF();
        RectF rect1 = new RectF();
        switch (type) {
            case All:
                rect.left = 0;
                rect.right = 0;
                rect.top = 0;
                rect.bottom = 0;
                break;
            case TOP:
                rect.left = mRect.left;
                rect.right = mRect.right;
                rect.top = mRect.height() / 2;
                rect.bottom = mRect.bottom + mShadowRadius;
                rect1.left = mRect.left;
                rect1.right = mRect.right;
                rect1.top = mRect.height() / 2;
                rect1.bottom = mRect.bottom;
                break;
            case BOTTOM:
                rect.left = mRect.left;
                rect.right = mRect.right;
                rect.top = mRect.top - mShadowRadius;
                rect.bottom = mRect.height() / 2;

                rect1.left = mRect.left;
                rect1.right = mRect.right;
                rect1.top = mRect.top;
                rect1.bottom = mRect.height() / 2;

                break;
            case LEFT:
                rect.left = mRect.width() / 2;
                rect.right = mRect.right + mShadowRadius;
                rect.top = mRect.top;
                rect.bottom = mRect.bottom;

                rect1.left = mRect.width() / 2;
                rect1.right = mRect.right ;
                rect1.top = mRect.top;
                rect1.bottom = mRect.bottom;

                break;
            case RIGHT:
                rect.left = mRect.left-mShadowRadius;
                rect.right = mRect.width() / 2;
                rect.top = mRect.top;
                rect.bottom = mRect.bottom;

                rect1.left = mRect.left;
                rect1.right = mRect.width() / 2;
                rect1.top = mRect.top;
                rect1.bottom = mRect.bottom;
                break;

            default:
                break;
        }

        if (mShape == SHAPE_ROUND) {
            /**
             * 绘制全圆角阴影
             */
            canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mShadowPaint);
            canvas.drawRect(rect, mPaint);
            canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mBgPaint);
        } else if (mShape == SHAPE_ROUND_PART) {
            /**
             * 绘制部分圆角阴影
             */
            canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mShadowPaint);
            canvas.drawRect(rect, mPaint);
            canvas.drawRoundRect(mRect, mShapeRadius, mShapeRadius, mBgPaint);
            canvas.drawRect(rect1, mBgPartPaint);
        } else {
            /**
             * 绘制圆形阴影
             */
            canvas.drawCircle(mRect.centerX(), mRect.centerY(), Math.min(mRect.width(), mRect.height()) / 2, mShadowPaint);
            canvas.drawRect(rect, mPaint);
            canvas.drawCircle(mRect.centerX(), mRect.centerY(), Math.min(mRect.width(), mRect.height()) / 2, mBgPaint);
        }
    }

    @Override
    public void setAlpha(int alpha) {
        mShadowPaint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(@Nullable ColorFilter colorFilter) {
        mShadowPaint.setColorFilter(colorFilter);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }

    public static void setShadowDrawable(View view) {
        ShadowDrawableUtil drawable = new Builder()
                .setType(TypeEnum.BOTTOM)
                .builder();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, drawable);
    }

    public static void setShadowDrawable(View view, String bgColor) {
        ShadowDrawableUtil drawable = new Builder()
                .setType(TypeEnum.BOTTOM)
                .setBgColor(bgColor)
                .builder();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, drawable);
    }

    public static void setShadowDrawable(View view, TypeEnum type) {
        ShadowDrawableUtil drawable = new Builder()
                .setType(type)
                .builder();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, drawable);
    }

    public static void setShadowDrawableCircle(View view) {
        ShadowDrawableUtil drawable = new Builder()
                .setShape(SHAPE_CIRCLE)
                .setType(TypeEnum.All)
                .builder();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, drawable);
    }

    public static void setShadowDrawableTop(View view, String shapeColor) {
        ShadowDrawableUtil drawable = new Builder()
                .setShape(SHAPE_ROUND_PART)
                .setType(TypeEnum.TOP)
                .setShadowColor(shapeColor)
                .builder();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, drawable);
    }

    public static void setShadowDrawable(View view, String shapeColor, int shapeRadius, String shadowColor, int shadowRadius) {
        ShadowDrawableUtil drawable = new Builder()
                .setBgColor(shapeColor)
                .setShapeRadius(shapeRadius)
                .setShadowColor(shadowColor)
                .setShadowRadius(shadowRadius)
                .builder();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, drawable);
    }

    public static void setShadowDrawable(View view, int shape, TypeEnum type, String shapeColor, int shapeRadius, String shadowColor, int shadowRadius) {
        ShadowDrawableUtil drawable = new Builder()
                .setBgColor(shapeColor)
                .setShapeRadius(shapeRadius)
                .setShadowColor(shadowColor)
                .setShadowRadius(shadowRadius)
                .setShape(shape)
                .setType(type)
                .builder();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, drawable);
    }

    public static void setShadowDrawable(View view, int shape, TypeEnum type, String[] shapeColor, int shapeRadius, String shadowColor, int shadowRadius) {
        ShadowDrawableUtil drawable = new Builder()
                .setBgColor(shapeColor)
                .setShapeRadius(shapeRadius)
                .setShadowColor(shadowColor)
                .setShadowRadius(shadowRadius)
                .setShape(shape)
                .setType(type)
                .builder();
        view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        ViewCompat.setBackground(view, drawable);
    }


    private static class Builder {
        private int mShape;
        private int mShapeRadius;
        private int mShadowColor;
        private int mShadowRadius;
        private int[] mBgColor;
        private TypeEnum type;

        private Builder() {
            mShape = SHAPE_ROUND;
            type = TypeEnum.All;
            mShapeRadius = 17;
            mShadowColor = Color.parseColor("#FF0000");
            mShadowRadius = 4;
            mBgColor = new int[1];
            mBgColor[0] = Color.GREEN;
        }

        private Builder setShape(int mShape) {
            this.mShape = mShape;
            return this;
        }

        private Builder setShapeRadius(int ShapeRadius) {
            this.mShapeRadius = ShapeRadius;
            return this;
        }

        private Builder setShadowColor(String shadowColor) {

            this.mShadowColor = Color.parseColor(shadowColor);
            return this;
        }

        private Builder setShadowRadius(int shadowRadius) {
            this.mShadowRadius = shadowRadius;
            return this;
        }

        private Builder setBgColor(String bgColor) {
            this.mBgColor[0] = Color.parseColor(bgColor);
            return this;
        }

        private Builder setType(TypeEnum type) {
            this.type = type;
            return this;
        }


        private Builder setBgColor(String[] bgColors) {
            if (bgColors != null && bgColors.length > 0) {
                int[] b = new int[bgColors.length];
                for (int i = 0; i < bgColors.length; i++) {
                    b[i] = Color.parseColor(bgColors[i]);
                }
                this.mBgColor = b;
            }

            return this;
        }

        private ShadowDrawableUtil builder() {
            return new ShadowDrawableUtil(mShape, type, mBgColor, dp2px(mShapeRadius), mShadowColor, dp2px(mShadowRadius));
        }
    }

    private static int dp2px(float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, ShadowApplication.getApplication().getResources().getDisplayMetrics());
    }
}


如有意见和建议,及时沟通。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,271评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,275评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,151评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,550评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,553评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,559评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,924评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,580评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,826评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,578评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,661评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,363评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,940评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,926评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,156评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,872评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,391评论 2 342