需要为图像视图设置色调…我使用它的方式如下:

imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);

但这并没有改变……


当前回答

从棒棒糖开始,还有一个用于BitmapDrawables的着色方法,它与新的Palette类一起工作:

(ColorStateList tint)

and

setTintMode (portterduff . setintmode)模式tintMode)

在旧版本的Android上,你现在可以使用DrawableCompat库

其他回答

在android中以编程方式设置图像视图的色调

我有两个方法为android:

1)

imgView.setColorFilter(context.getResources().getColor(R.color.blue));

2)

 DrawableCompat.setTint(imgView.getDrawable(),
                     ContextCompat.getColor(context, R.color.blue));

我希望我能帮到大家:-)

免责声明:这不是本文的答案。但它是这个问题的答案,即如何重置可绘制或imageview的颜色/色调。对不起,把这个放在这里,因为这个问题不接受答案,请参阅这篇文章的答案。所以,把它加在这里,这样人们在寻找解决方案时就会得到这个。

正如@RRGT19在这个回答的评论中提到的。我们可以使用setImageTintList()和传递null作为tintList来重置颜色。它神奇地为我工作。

ImageViewCompat.setImageTintList(imageView, null)

因为第一个答案对我不起作用:

//get ImageView
ImageView myImageView = (ImageView) findViewById(R.id.iv);

//colorid is the id of a color defined in values/colors.xml
myImageView.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(getApplicationContext(), R.color.colorid)));

这似乎只适用于API 21+,但对我来说这不是问题。不过,您可以使用ImageViewCompat来解决这个问题。

我希望我能帮到大家:-)

如果你的颜色有十六进制透明度,使用下面的代码。

ImageViewCompat.setImageTintMode(imageView, PorterDuff.Mode.SRC_ATOP);
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(Color.parseColor("#80000000")));

清除色彩

ImageViewCompat.setImageTintList(imageView, null);

对我来说,这个代码是有效的。我使用它与卡片和图像视图,但我认为它可以在任何视图改变他们的色调颜色。 cardBookmark是我的cardView。

var cardDrawable: Drawable = binding.cardBookmark.background
cardDrawable = DrawableCompat.wrap(cardDrawable)
DrawableCompat.setTint(cardDrawable, resources.getColor(R.color.shuffleColor))
binding.cardBookmark.background = cardDrawable