android 在 xml 中设置的 imageview 色调以编程方式覆盖在 DrawableCompat 上设置的色调

android imageview's tint set in xml overrides tint set on DrawableCompat programmatically

改天,又一个问题!我已经设置了一个带有色调的 ImageView,如下所示:

<ImageView
    android:id="@+id/image_money"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:padding="3dp"
    android:src="@drawable/ic_money_icon"
    android:tint="#c7c7c7"/>

而且我需要恢复或设置 tintColor,所以我使用 DrawableCompat 如下:

Drawable imagem = holder.view.getContext().getResources().getDrawable(R.drawable.ic_money_icon);
imagem = DrawableCompat.wrap(imagem);
imagem = imagem.mutate();
DrawableCompat.setTint(imagem,Color.parseColor("#43a085"));
holder.imageDebito.setImageDrawable(imagem);

我检查了 imagem 上的位图,它看起来应该是我设置的颜色,但是当应用到 ImageView imageDebito 时,它恢复到在XML。如果我拍摄视图中未使用的不同图像,应用色调,然后将其设置在 ImageView 上,它会从 XML 获得相同的色调...我尝试设置 setImageTintList(),但 API 级别 17 不可用...

因此,我需要删除色调 属性,或者在 xml 的 ImageView 图像上强制图像色调。

使用setColorFilter()方法。

Drawable myIcon = getResources().getDrawable( R.drawable.button ); 
ColorFilter filter = new LightingColorFilter( Color.BLACK, Color.BLACK);
myIcon.setColorFilter(filter);

编辑:刚刚改进了格式...