概念
Tint,即着色,API 21之后,Android支持可以给对应的Drawable着上特定的颜色。使用着色,可以有效的减小APK包的大小。比如,只需要一张特定的背景图片,运行时设置不同的tintColor达到想要的效果。在使用appcompat-v7包的时候,为了实现Material Design
的效果,我们会去设置主题里的几个颜色,重要的比如primaryColor,colorControlNormal
,colorControlActived 等等,而我们使用的一些组件,比如EditText的背景就会自动变成我们想要的背景颜色,其实EditText使用的背景图片就是单独的一张,只是使用了着色而已。着色的原理其实就是在Drawable上设置了对应的ColorFilter,具体是一个PorterDuffColorFilter实例。PorterDuffColorFilter可以指定一个颜色值和一个PorterDuff.Mode值。
public abstract class Drawable {
public void setTint(int tintColor) {}
//可以创建自己的ColorStateList对象,对应不同state下不同的着色
public void setTintList(ColorStateList tint) {}
public void setTintMode(PorterDuff.Mode tintMode) {} //默认为SRC_IN
}
使用
API 21以上,直接调用对应Drawable的方法即可,对于API21以下,可以使用v4包中的DrawableCompat类来完成。
public class DrawableCompat {
public static void setTint(Drawable drawable, int tint) {}
public static void setTintList(Drawable drawable, ColorStateList tint) {}
public static void setTintMode(Drawable drawable, PorterDuff.Mode tintMode) {}
}
public static Drawable tintDrawable(Drawable drawable, ColorStateList colors) {
final Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTintList(wrappedDrawable, colors);
return wrappedDrawable;
}
注意
如果存在多次引用同一个drawable对象的情况下,对Drawable进行着色,则其他的引用也会改变。Android为了节约内存,每一个Drawable都会有一个可重用的ConstantState对象,这个对象包含了这个Drawable的通用属性,具体的属性由具体的Drawable实例对象决定。如,BitmapDrawble包含一个BitmapState对象,这个BitmapState对象中又包含一个Bitmap对象等其他属性。当加载一张图片之后,AOS会缓存对应的BitmapState对象,等下次在加载相同的资源图片时,仅仅是使用一个新建的BitmapDrawable对象来包含缓存中的BitmapState,达到一个共享以减少内存消耗的目的。
解决上面问题的方法如下
final Drawable originBitmapDrawable = getResources().getDrawable(R.drawable.ic_account_circle_black_18dp).mutate();
/* Make this drawable mutable. This operation cannot be reversed.
A mutable * drawable is guaranteed to not share its state with any other drawable. *
This is especially useful when you need to modify properties of drawables * loaded from resources.
By default, all drawables instances loaded from * the same resource share a common state; if you modify the state of one * instance, all the other instances will receive the same modification. * *
Calling this method on a mutable Drawable will have no effect. */
public Drawable mutate() { }
这样以后,当我们给Drawable着色时,就不是对相同的ConstantState对象操作,从而避免影响到其他Drawable。
参考
Drawable mutations
浅谈Tint
Android 着色器 Tint 研究
PorterDuff Mode 详解