Android:为三个图标之一着色

Android:为三个图标之一着色,android,drawable,android-drawable,android-resources,android-vectordrawable,Android,Drawable,Android Drawable,Android Resources,Android Vectordrawable,我需要3个图标:ContextCompat.getDrawable(this,R.drawable.my\u vector\u drawable) 第一种——不带色调,第二种——带色调,第三种——也不带色调 嗯 其中R.drawable.my_vector_drawable为白色数字 但结果是–3个带色调的图标(为什么?!) 例如,我试图设置ContextCompat.getColor(this,R.color.somecolor),结果是。。。两个带有色调的图标!图标2和3,以及第一个图标-没

我需要3个图标:
ContextCompat.getDrawable(this,R.drawable.my\u vector\u drawable)

第一种——不带色调,第二种——带色调,第三种——也不带色调

其中
R.drawable.my_vector_drawable
为白色数字

但结果是–3个带色调的图标(为什么?!)

例如,我试图设置
ContextCompat.getColor(this,R.color.somecolor)
,结果是。。。两个带有色调的图标!图标2和3,以及第一个图标-没有色调(为什么?!)

如何加载不可缓存的drawable?或者如何解决这个问题?AppCompat 23.4.+

您必须使用您的绘图设备

现在您引用的是完全相同的源。一旦你改变了你的drawable,每一个都会有自己的状态

Drawable d = ContextCompat.getDrawable(this, R.drawable.my_vector_drawable).mutate();
发件人:

使此可绘制和可更改。此操作无法反转。一个可变的可绘制对象保证不会与任何其他可绘制对象共享其状态。当您需要修改从资源加载的可绘图项的属性时,这尤其有用。默认情况下,从同一资源加载的所有drawables实例共享一个公共状态;如果修改一个实例的状态,所有其他实例将收到相同的修改。对可变可绘制的调用此方法将无效


@azizbekian的答案是正确的,但在某些情况下,对每个可绘制的图形都这样做可能效率低下。请查看以了解更多详细信息

我建议你用它,而不是改变你的画。它将管理着色绘图的缓存,以便在需要时只创建一次,然后以节省内存的方式缓存以供后续使用。这是一个单一的类,你可以从中获取

用法
哦,你说得对,我忘了这个方法!非常感谢。
Drawable d = ContextCompat.getDrawable(this, R.drawable.my_vector_drawable).mutate();
// Get an instance
final TintedIconCache cache = TintedIconCache.getInstance();

// Will be fetched from the resources
Drawable backIcon = cache.fetchTintedIcon(context, R.drawable.icon, R.color.black));

// Will be fetched from the resources as well
Drawable bleuIcon = cache.fetchTintedIcon(context, R.drawable.icon, R.color.bleu));
   
// Will be fetched from the cache, with no mutation!!!
Drawable backIconTwo = cache.fetchTintedIcon(context, R.drawable.icon, R.color.back));