需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
需要为图像视图设置色调…我使用它的方式如下:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并没有改变……
当前回答
在我尝试了所有的方法后,他们都不适合我。
我得到的解决方案,特别是在情况下,如果你改变颜色通过任何colorPicker库返回一个整数值的selectedColor。
widgetIcon是ImageView selectedColor是来自colorPicker的颜色
var imageDrawable: Drawable = widgetIcon.background
imageDrawable = DrawableCompat.wrap(imageDrawable)
DrawableCompat.setTint(imageDrawable, selectedColor)
widgetIcon.background = imageDrawable
其他回答
在我使用的java中
imageView.setImageTintList(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.red)));
更新: @ADev在他的回答中有更新的解决方案,但他的解决方案需要更新的支持库- 25.4.0或以上。
你可以改变色调,很容易在代码中通过:
imageView.setColorFilter(颜色。Argb (255, 255, 255, 255));//白色色调
如果你想要色彩鲜艳的话
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);
对于可绘制的向量
imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);
如果你正在改变对焦的色调,请试试这个
DrawableCompat.setTint(imgView.getDrawable(),
ContextCompat.getColor(context, R.color.blue));
大多数答案指的是使用setColorFilter,这不是最初的问题。
用户@Tad的答案是正确的,但它只适用于API 21+。
要设置所有Android版本的色调,使用ImageViewCompat:
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));
注意,在这种情况下,yourTint必须是一个“颜色int”。如果你有一个像r。color这样的颜色资源。蓝色,你需要先加载颜色int:
ContextCompat.getColor(context, R.color.blue);
简单一行
imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));