522

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

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

但它并没有改变...

4

24 回答 24

1162

更新
@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);
于 2013-11-21T13:13:04.527 回答
359

大多数答案是指使用setColorFilterwhich 不是最初要求的。

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

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

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

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

ContextCompat.getColor(context, R.color.blue);
于 2017-08-08T15:00:17.407 回答
68

这对我有用

mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));
于 2015-10-14T17:59:01.067 回答
39

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

如果此行在您的活动中:

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);

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

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

于 2014-08-12T17:05:37.027 回答
25

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

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

用法:-

imageView.setTint(R.color.tintColor)
于 2019-05-08T07:34:31.410 回答
25

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

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

imgEstadoBillete.setColorFilter(context.getResources().getColor(R.color.green),PorterDuff.Mode.SRC_IN);
于 2016-10-25T13:53:21.263 回答
21

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

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

清除色调

ImageViewCompat.setImageTintList(imageView, null);
于 2018-05-04T11:43:26.970 回答
17

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

public void setTintList (ColorStateList tint)

and

public void setTintMode (PorterDuff.Mode tintMode)

On older versions of Android, you can now use the DrawableCompat library

于 2015-01-13T18:43:08.733 回答
16

简单的一行

imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));
于 2018-03-27T05:08:06.573 回答
14

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

您可以使用上述任何方法使其工作。

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

于 2015-12-27T09:08:54.563 回答
13

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

我有两种适用于 android 的方法:

1)

imgView.setColorFilter(context.getResources().getColor(R.color.blue));

2)

 DrawableCompat.setTint(imgView.getDrawable(),
                     ContextCompat.getColor(context, R.color.blue));

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

于 2019-05-03T10:15:30.233 回答
11

我发现我们可以将颜色选择器用于 tint attr:

mImageView.setEnabled(true);

活动主.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" />

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>
于 2017-06-14T09:41:36.437 回答
10

添加到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 项目中使用的功能!

于 2019-04-25T08:14:45.967 回答
10

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))
}
于 2020-05-04T13:17:43.940 回答
9

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

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

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

于 2017-10-12T20:43:30.453 回答
8

从 Lollipop 开始,您可以使用一种名为的方法ImageView#setImageTintList()……其优点是它采用 aColorStateList而不是仅一种颜色,从而使图像的色调状态感知。

在棒棒糖之前的设备上,您可以通过为可绘制对象着色并将其设置为ImageView可绘制的图像来获得相同的行为:

ColorStateList csl = AppCompatResources.getColorStateList(context, R.color.my_clr_selector);
Drawable drawable = DrawableCompat.wrap(imageView.getDrawable());
DrawableCompat.setTintList(drawable, csl);
imageView.setImageDrawable(drawable);
于 2016-10-26T04:04:24.637 回答
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);
于 2014-04-28T12:16:32.270 回答
3

如果您想将选择器设置为您的色调:

ImageViewCompat.setImageTintList(iv, getResources().getColorStateList(R.color.app_icon_click_color));
于 2019-03-15T10:02:52.767 回答
3

不要使用PoterDuff.Mode,使用setColorFilter()它适用于所有人。

ImageView imageView = (ImageView) listItem.findViewById(R.id.imageView);
imageView.setColorFilter(getContext().getResources().getColor(R.color.msg_read));
于 2018-07-03T11:25:59.917 回答
3

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

此 API 需要颜色值而不是颜色资源 id,这就是您的语句不起作用的根本原因。

于 2015-10-08T15:09:23.070 回答
2

我在聚会上迟到了,但我没有在上面看到我的解决方案。我们也可以通过 设置色调颜色setImageResource()(我的 minSdkVersion 是 24)。

因此,首先,您需要创建一个选择器并将其保存在/drawable资产文件夹中(我称之为ic_color_white_green_search.xml

<!-- Focused and not pressed -->
<item android:state_focused="true"
      android:state_pressed="false">

    <bitmap android:src="@drawable/ic_search"
            android:tint="@color/branding_green"/>
</item>

<!-- Focused and pressed -->
<item android:state_focused="true"
      android:state_pressed="true">

    <bitmap android:src="@drawable/ic_search"
            android:tint="@color/branding_green"/>
</item>

<!-- Default -->
<item android:drawable="@drawable/ic_search"/>

然后在这样的代码中设置它:

val icon = itemView.findViewById(R.id.icon) as ImageButton
icon.setImageResource(R.drawable.ic_color_white_green_search)
于 2018-12-13T19:27:30.223 回答
0

对我来说,这段代码有效。我将它与卡片和图像视图一起使用,但我认为它可以在任何视图中更改其色调颜色。cardBookmark 是我的cardView。

var cardDrawable: Drawable = binding.cardBookmark.background
cardDrawable = DrawableCompat.wrap(cardDrawable)
DrawableCompat.setTint(cardDrawable, resources.getColor(R.color.shuffleColor))
binding.cardBookmark.background = cardDrawable
于 2021-07-01T22:00:48.073 回答
0

免责声明:这不是这篇文章的答案。但这是这个问题的答案,即如何重置可绘制或图像视图的颜色/色调。抱歉,将其放在这里,因为该问题不接受答案并参考此帖子以获取答案。所以,在这里添加它,以便寻找解决方案的人可能会看到这个。

正如@RRGT19答案的评论中提到的那样。我们可以使用setImageTintList()null作为 tintList 传递来重置颜色。它对我来说很神奇。

ImageViewCompat.setImageTintList(imageView, null)
于 2022-02-18T06:14:15.127 回答
-4

不是确切的答案,而是一个更简单的选择:

  • 在图像顶部放置另一个视图
  • 更改视图的alpha值,但您希望(以编程方式)获得所需的效果。

这是一个片段:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="@dimen/height120"
        android:contentDescription="@string/my_description"
        android:scaleType="fitXY"
        android:src="@drawable/my_awesome_image"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="@dimen/height120"
        android:alpha="0.5"
        android:background="@color/my_blue_color"/>
</FrameLayout>
于 2015-08-24T05:52:05.260 回答