如何在android中以编程方式为图像视图设置色调?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20121938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:20:14  来源:igfitidea点击:

How to set tint for an image view programmatically in android?

androidimageviewtint

提问by Hardik

Need to set tint for an image view... I am using it the following way:

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

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

But it doesn't change...

但它不会改变...

回答by Hardik

You can change the tint, quite easily in code via:

您可以通过以下方式在代码中轻松更改色调:

imageView.setColorFilter(Color.argb(255, 255, 255, 255));// White Tint

imageView.setColorFilter(Color.argb(255, 255, 255, 255));// 白色色调

If you want color tint then

如果你想要色调,那么

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);

For Vector Drawable

对于矢量可绘制

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);

UPDATE:
@ADev has newer solution in his answer here, but his solution requires newer support library - 25.4.0 or above.

UPDATE
@ADev在他的回答较新的解决方案在这里,但他的解决方案需要新的支持库- 25.4.0或更高版本。

回答by ADev

Most answers refer to using setColorFilterwhich is not what was originally asked.

大多数答案是指使用setColorFilter哪个不是最初提出的问题。

The user @Tad has his answerin the right direction but it only works on API 21+.

用户@Tad的答案是正确的,但它只适用于 API 21+。

To set the tint on all Android versions, use the ImageViewCompat:

要在所有 Android 版本上设置色调,请使用ImageViewCompat

ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));

Note that yourTintin this case must be a "color int". If you have a color resource like R.color.blue, you need to load the color int first:

请注意,yourTint在这种情况下必须是“color int”。如果您有像 一样的颜色资源R.color.blue,则需要先加载颜色 int:

ContextCompat.getColor(context, R.color.blue);

回答by toobsco42

This worked for me

这对我有用

mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));

回答by CrepeGoat

@Hardik has it right. The other error in your code is when you reference your XML-defined color. You passed only the id to the setColorFiltermethod, when you should use the ID to locate the color resource, and pass the resourceto the setColorFiltermethod. Rewriting your original code below.

@Hardik 说得对。代码中的另一个错误是当您引用 XML 定义的颜色时。您只将 id 传递给setColorFilter方法,什么时候应该使用 ID 来定位颜色资源,并将资源传递给setColorFilter方法。在下面重写您的原始代码。

If this line is within your activity:

如果此行在您的活动范围内:

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

Else, you need to reference your main activity:

否则,您需要引用您的主要活动:

Activity main = ...
imageView.setColorFilter(main.getResources().getColor(R.color.blue), android.graphics.PorterDuff.Mode.MULTIPLY);

Note that this is also true of the other types of resources, such as integers, bools, dimensions, etc. Except for string, for which you can directly use getString()in your Activity without the need to first call getResources()(don't ask me why).

请注意,其他类型的资源也是如此,例如整数、布尔值、维度等。 除了字符串,您可以直接getString()在您的 Activity 中使用,无需先调用getResources()(不要问我为什么) .

Otherwise, your code looks good. (Though I haven't investigated the setColorFiltermethod too much...)

否则,您的代码看起来不错。(虽然我没有研究setColorFilter太多方法......)

回答by Catluc

After i tried all methods and they did not work for me.

在我尝试了所有方法之后,它们对我不起作用。

I get the solution by using another PortDuff.MODE.

我通过使用另一个 PortDuff.MODE 得到了解决方案。

imgEstadoBillete.setColorFilter(context.getResources().getColor(R.color.green),PorterDuff.Mode.SRC_IN);

回答by Tad

Beginning with Lollipop, there is also a tintmethod for BitmapDrawables that works with the new Palette class:

从 Lollipop 开始,还有一个BitmapDrawables的tint方法可以与新的 Palette 类一起使用:

public void setTintList (ColorStateList tint)

public void setTintList (ColorStateList tint)

and

public void setTintMode (PorterDuff.Mode tintMode)

public void setTintMode (PorterDuff.Mode tintMode)

On older versions of Android, you can now use the DrawableCompatlibrary

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

回答by android developer

Try this. It should work on all Android versions that the support library supports:

尝试这个。它应该适用于支持库支持的所有 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;
}

You can use any of the above to make it work.

您可以使用上述任何一种来使其工作。

You can read about more interesting features of DrawableCompat on the docs, here.

您可以在此处的文档中阅读有关 DrawableCompat 的更多有趣功能。

回答by Sai

If your color has hex transparency, use the below code.

如果您的颜色具有十六进制透明度,请使用以下代码。

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

To clear the tint

清除色调

ImageViewCompat.setImageTintList(imageView, null);

回答by Manohar Reddy

Better simplified extension function thanks to ADev

借助 ADev更好地简化扩展功能

fun ImageView.setTint(@ColorRes colorRes: Int) {
    ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(ContextCompat.getColor(context, colorRes)))
}

Usage:-

用法:-

imageView.setTint(R.color.tintColor)

回答by Gautam Surani

Simple and one line

简单一行

imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));