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

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

但这并没有改变……


当前回答

我在派对上迟到了,但我没有看到我的解决方案。我们能够通过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)

其他回答

不要使用波特达夫。模式, 使用setColorFilter()它适用于所有情况。

ImageView imageView = (ImageView) listItem.findViewById(R.id.imageView);
imageView.setColorFilter(getContext().getResources().getColor(R.color.msg_read));

从棒棒糖开始,还有一个用于BitmapDrawables的着色方法,它与新的Palette类一起工作:

(ColorStateList tint)

and

setTintMode (portterduff . setintmode)模式tintMode)

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

免责声明:这不是本文的答案。但它是这个问题的答案,即如何重置可绘制或imageview的颜色/色调。对不起,把这个放在这里,因为这个问题不接受答案,请参阅这篇文章的答案。所以,把它加在这里,这样人们在寻找解决方案时就会得到这个。

正如@RRGT19在这个回答的评论中提到的。我们可以使用setImageTintList()和传递null作为tintList来重置颜色。它神奇地为我工作。

ImageViewCompat.setImageTintList(imageView, null)

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

ImageViewCompat.setImageTintList(iv, getResources().getColorStateList(R.color.app_icon_click_color));

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