android应用图标快速换色-tint

1.应用场景

项目中的大量图标需要统一换色,可以用下面的方式, 只要自己封装一个工具类就可以了

1.xml中使用tint


2.代码中使用tint

Drawable drawable = ContextCompat.getDrawable(this,R.mipmap.icon);
//简单的使用tint改变drawable颜色
Drawable drawable1 = getTintDrawable(drawable,ContextCompat.getColor(this,R.color.pink));
imageView.setImageDrawable(drawable1);

private Drawable getTintDrawable(Drawable drawable,@ColorInt int color) {
        Drawable.ConstantState state = drawable.getConstantState();
        Drawable drawable1 = DrawableCompat.wrap(state == null ? drawable : state.newDrawable()).mutate();
        drawable1.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        DrawableCompat.setTint(drawable1, color);
        return drawable1;
    }

参考文章:

http://www.jianshu.com/p/6bd7dd1cd491
https://github.com/afeilo/TintDemo/tree/master

你可能感兴趣的:(android应用图标快速换色-tint)