需要为图像视图设置色调... 我按以下方式使用它:
imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
但这并不改变...
更新:
@ADev在他的回答这里中提供了一个更新的解决方案,但他的解决方案需要更新的支持库 - 25.4.0或以上。
您可以通过以下代码轻松更改色调:
imageView.setColorFilter(Color.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);
android:tint
可以在所有 Android 版本上使用。也许你指的是 drawableTint
? - finstas大多数答案都提到使用setColorFilter
,这不是最初的问题所在。
@Tad用户的答案指向正确方向,但只适用于API 21+。
要在所有Android版本上设置色调,请使用ImageViewCompat
:
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));
请注意,yourTint
在这种情况下必须是“颜色 int”。 如果您有像R.color.blue
这样的颜色资源,则需要先加载颜色 int:
ContextCompat.getColor(context, R.color.blue);
ImageView
实例或AppCompatImageView
子类。 - Louis CAD这对我起作用了
mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));
mImageView.setColorFilter(getContext().getResources().getColor(R.color.green_500));
- Biskrem Muhammad@Hardik 的回答是正确的。你代码中的另一个错误是当引用 XML 定义的颜色时。你只向 setColorFilter
方法传递了 ID,而应该使用该 ID 来定位颜色资源,并将 资源 传递给 setColorFilter
方法。下面是重写后的原始代码。
如果此行代码在您的 Activity 中:
imageView.setColorFilter(getResources().getColor(R.color.blue), android.graphics.PorterDuff.Mode.MULTIPLY);
否则,您需要引用您的主活动:
Activity main = ...
imageView.setColorFilter(main.getResources().getColor(R.color.blue), android.graphics.PorterDuff.Mode.MULTIPLY);
请注意,其他类型的资源(如整数、布尔值、尺寸等)也是如此。除了字符串之外,您可以直接在Activity中使用getString()
,而无需先调用getResources()
(不要问我为什么)。否则,您的代码看起来很不错。(虽然我没有太深入地研究setColorFilter
方法...)多亏了ADev,现在有更好的简化扩展函数了。
fun ImageView.setTint(@ColorRes colorRes: Int) {
ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(ContextCompat.getColor(context, colorRes)))
}
使用方法:
imageView.setTint(R.color.tintColor)
在我尝试了所有方法且它们对我都无效后。
我通过使用另一个PortDuff.MODE获得了解决方案。
imgEstadoBillete.setColorFilter(context.getResources().getColor(R.color.green),PorterDuff.Mode.SRC_IN);
如果您的颜色具有十六进制透明度,请使用下面的代码。
ImageViewCompat.setImageTintMode(imageView, PorterDuff.Mode.SRC_ATOP);
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(Color.parseColor("#80000000")));
去除色调
ImageViewCompat.setImageTintList(imageView, null);
img
是 ImageView 类型。 - Sai简洁明了,一目了然
imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));
从Lollipop开始,BitmapDrawables也有了一个tint方法,可以与新的Palette类一起使用:
public void setTintList(ColorStateList tint)
和
public void setTintMode(PorterDuff.Mode tintMode)
在旧版本的Android上,现在可以使用DrawableCompat库。
试试这个。它应该适用于支持库支持的所有Android版本:
public static Drawable getTintedDrawableOfColorResId(@NonNull Context context, @NonNull Bitmap inputBitmap, @ColorRes int colorResId) {
return getTintedDrawable(context, new BitmapDrawable(context.getResources(), inputBitmap), ContextCompat.getColor(context, colorResId));
}
public static Drawable getTintedDrawable(@NonNull Context context, @NonNull Bitmap inputBitmap, @ColorInt int color) {
return getTintedDrawable(context, new BitmapDrawable(context.getResources(), inputBitmap), color);
}
public static Drawable getTintedDrawable(@NonNull Context context, @NonNull Drawable inputDrawable, @ColorInt int color) {
Drawable wrapDrawable = DrawableCompat.wrap(inputDrawable);
DrawableCompat.setTint(wrapDrawable, color);
DrawableCompat.setTintMode(wrapDrawable, PorterDuff.Mode.SRC_IN);
return wrapDrawable;
}
你可以使用以上任何一种来使其工作。imageView.getBackground()
来获取可绘制对象,因为imageView.getDrawable()
返回的是null。 - Rock Lee
imageView.setImageDrawable(drawable); // 这里可以使用任何颜色 - flame3