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

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

但这并没有改变……


当前回答

加上ADev的答案(在我看来是最正确的),因为Kotlin的广泛采用,以及它有用的扩展函数:

fun ImageView.setTint(context: Context, @ColorRes colorId: Int) {
    val color = ContextCompat.getColor(context, colorId)
    val colorStateList = ColorStateList.valueOf(color)
    ImageViewCompat.setImageTintList(this, colorStateList)
}

我认为这是一个在任何Android项目中都有用的功能!

其他回答

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

(ColorStateList tint)

and

setTintMode (portterduff . setintmode)模式tintMode)

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

Random random=new Random;
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
ColorFilter cf = new PorterDuffColorFilter(Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255)),Mode.OVERLAY);

imageView.setImageResource(R.drawable.ic_bg_box);
imageView.setColorFilter(cf);

正如@milosmns所说,你应该使用 imageView.setColorFilter (getResouces () .getColor (R.color.blue) android.graphics.PorterDuff.Mode.MULTIPLY);

这个API需要颜色值而不是颜色资源id,这就是为什么你的语句没有工作的根本原因。

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

//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来解决这个问题。

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

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

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

ImageViewCompat.setImageTintList(imageView, null)