1.应用场景
项目中的大量图标需要统一换色,可以用下面的方式, 只要自己封装一个工具类就可以了
1.xml中使用tint
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image2"
android:src="@mipmap/icon"
android:tint="#FFCDD2"
android:clickable="true"
/>
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;
}
参考文章:
//www.greatytc.com/p/6bd7dd1cd491
https://github.com/afeilo/TintDemo/tree/master