美文网首页
Android Drawable 颜色转换

Android Drawable 颜色转换

作者: 醉红尘_acb4 | 来源:发表于2021-09-26 19:22 被阅读0次

    方法

     public static final class TintedBitmapDrawable extends BitmapDrawable {
            private int tint;
            private int alpha;
    
            public TintedBitmapDrawable(final Resources res, final Bitmap bitmap, final int tint) {
                super(res, bitmap);
                this.tint = tint;
                this.alpha = Color.alpha(tint);
            }
    
            public TintedBitmapDrawable(final Resources res, final int resId, final int tint) {
                super(res, BitmapFactory.decodeResource(res, resId));
                this.tint = tint;
                this.alpha = Color.alpha(tint);
            }
    
            public void setTint(final int tint) {
                this.tint = tint;
                this.alpha = Color.alpha(tint);
            }
    
            @Override
            public void draw(final Canvas canvas) {
                final Paint paint = getPaint();
                if (paint.getColorFilter() == null) {
                    paint.setColorFilter(new LightingColorFilter(tint, 0));
                    paint.setAlpha(alpha);
                }
                super.draw(canvas);
    
            }
    
        }
    

    使用

    BitmapDrawable drawable = new TintedBitmapDrawable(getResources(), R.mipmap.warning3, Color.parseColor("#fa3030"));
    img.setImageDrawable(drawable);

    相关文章

      网友评论

          本文标题:Android Drawable 颜色转换

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