动态给drawable上色

时间:2023-03-08 19:08:26

利用 PorterDuff 改变资源原有色值,从而实现只需要一个资源文件,就可以表示几种不同的状态,如在线或者离线等等

public Drawable colorDrawable(Resources res, @DrawableRes int drawableResId, @ColorRes int colorResId){
Drawable drawable = res.getDrawable(drawableResId);
int color = res.getColor(colorResId);
drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
return drawable;
}

如果项目中有大量这样的表示不同状态的相同图片资源,那么将大大减少APK的体积。

相关文章