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

355 android tint imageview

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

imageView.setColorFilter(R.color.blue,android.graphics.PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)

但它没有改变......

Har*_*dik 825

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

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

如果你想要色彩

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)

对于Vector Drawable

imageView.setColorFilter(ContextCompat.getColor(context, R.color.COLOR_YOUR_COLOR), android.graphics.PorterDuff.Mode.SRC_IN);
Run Code Online (Sandbox Code Playgroud)

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

  • 在xml中,android:tint ="@ color/blue" (12认同)
  • `android:tint`适用于所有Android版本.也许你在谈论'drawableTint`? (8认同)
  • PorterDuff.Mode.MULTIPLY在我使用PorterDuff.Mode.SRC_IN的情况下无法正常工作 (8认同)
  • 更多信息:[public final void setColorFilter(int color, PorterDuff.Mode mode)](http://developer.android.com/reference/android/widget/ImageView.html#setColorFilter%28int%2C%20android.graphics.PorterDuff .模式%29) (2认同)

ADe*_*Dev 181

大多数答案都是指使用setColorFilter哪种不是最初提出的问题.

用户@Tad 的答案朝着正确的方向,但它仅适用于API 21+.

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

ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));
Run Code Online (Sandbox Code Playgroud)

请注意,yourTint在这种情况下必须是"color int".如果你有一个颜色资源R.color.blue,你需要先加载颜色int:

ContextCompat.getColor(context, R.color.blue);
Run Code Online (Sandbox Code Playgroud)

  • 应该是接受的答案.请注意,它仅适用于具有AppCompat主题的xml`ImageView`实例或适用于`AppCompatImageView`子类的实例. (4认同)

too*_*o42 41

这对我有用

mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));
Run Code Online (Sandbox Code Playgroud)

  • 是的,对我也有用,没有第二个参数..它也可以去`mImageView.setColorFilter(getContext().getResources().getColor(R.color.green_500));` (2认同)

Cre*_*oat 35

@Hardik没错.代码中的另一个错误是当您引用XML定义的颜色时.您只传递了ID的setColorFilter方法,当你应该使用ID来定位颜色资源,并通过资源setColorFilter方法.重写下面的原始代码.

如果此行在您的活动中:

imageView.setColorFilter(getResources().getColor(R.color.blue), android.graphics.PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)

否则,您需要参考您的主要活动:

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

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

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


Cat*_*luc 19

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

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

imgEstadoBillete.setColorFilter(context.getResources().getColor(R.color.green),PorterDuff.Mode.SRC_IN);
Run Code Online (Sandbox Code Playgroud)


Tad*_*Tad 13

从Lollipop开始,还有一个适用于新Palette类的BitmapDrawables 的tint方法:

public void setTintList(ColorStateList tint)

public void setTintMode(PorterDuff.Mode tintMode)

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

  • 实际上,支持库确实支持它.看到我的回答:http://stackoverflow.com/a/34479043/878126 (2认同)

and*_*per 11

试试这个.它应该适用于支持库支持的所有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;
}
Run Code Online (Sandbox Code Playgroud)

您可以使用上述任何一项来使其工作.

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


Abh*_*n01 11

用于在 android 中以编程方式为图像视图设置色调

我有两种 android 方法:

1)

imgView.setColorFilter(context.getResources().getColor(R.color.blue));
Run Code Online (Sandbox Code Playgroud)

2)

 DrawableCompat.setTint(imgView.getDrawable(),
                     ContextCompat.getColor(context, R.color.blue));
Run Code Online (Sandbox Code Playgroud)

我希望我能帮助任何人:-)


Gau*_*ani 10

简单而且一条线

imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));
Run Code Online (Sandbox Code Playgroud)


小智 10

kotlin中的扩展函数,用于设置和取消设置着色。

fun ImageView.setTint(@ColorRes color: Int?) {
  if (color == null) {
    ImageViewCompat.setImageTintList(this, null)
  } else {
    ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(ContextCompat.getColor(context, color)))
}}
Run Code Online (Sandbox Code Playgroud)

用法:yourImageView.setTint(R.color.white)用于设置和删除:yourImageView.setTint(null)


小智 9

添加到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)
}
Run Code Online (Sandbox Code Playgroud)

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


Nic*_*hek 8

我发现我们可以使用颜色选择器进行色调属性:

mImageView.setEnabled(true);
Run Code Online (Sandbox Code Playgroud)

活动_main.xml:

<ImageView
    android:id="@+id/image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_arrowup"
    android:tint="@color/section_arrowup_color" />
Run Code Online (Sandbox Code Playgroud)

section_arrowup_color.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/white" android:state_enabled="true"/>
    <item android:color="@android:color/black" android:state_enabled="false"/>
    <item android:color="@android:color/white"/>
</selector>
Run Code Online (Sandbox Code Playgroud)


Ale*_*ood 7

从Lollipop开始,有一种方法叫做ImageView#setImageTintList()你可以使用...的优点是它只需要一种ColorStateList颜色而不是一种颜色,从而使图像的色彩状态感知.

在前Lollipop设备上,您可以通过着色drawable然后将其设置为ImageView可绘制的图像来获得相同的行为:

ColorStateList csl = AppCompatResources.getColorStateList(context, R.color.my_clr_selector);
Drawable drawable = DrawableCompat.wrap(imageView.getDrawable());
DrawableCompat.setTintList(drawable, csl);
imageView.setImageDrawable(drawable);
Run Code Online (Sandbox Code Playgroud)


Fel*_*lix 7

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

//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)));
Run Code Online (Sandbox Code Playgroud)

这似乎仅在API 21+中有效,但是对我来说这不是问题。您可以使用ImageViewCompat解决该问题。

我希望我能帮助任何人:-)


Sai*_*Sai 7

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

ImageViewCompat.setImageTintMode(img,PorterDuff.Mode.SRC_ATOP);
ImageViewCompat.setImageTintList(img,ColorStateList.valueOf(Color.parseColor("#80000000")));
Run Code Online (Sandbox Code Playgroud)

清除色彩

ImageViewCompat.setImageTintList(img, null);
Run Code Online (Sandbox Code Playgroud)


Paw*_*ati 6

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);
Run Code Online (Sandbox Code Playgroud)


and*_*per 6

Kotlin 解决方案使用扩展功能,设置和取消设置着色:

fun ImageView.setTint(@ColorInt color: Int?) {
    if (color == null) {
        ImageViewCompat.setImageTintList(this, null)
        return
    }
    ImageViewCompat.setImageTintMode(this, PorterDuff.Mode.SRC_ATOP)
    ImageViewCompat.setImageTintList(this, ColorStateList.valueOf(color))
}
Run Code Online (Sandbox Code Playgroud)


Man*_*ddy 5

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

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

用法:-

imageView.setTint(R.color.tintColor)
Run Code Online (Sandbox Code Playgroud)