需要为图像视图设置色调......我使用它的方式如下:
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);
大多数答案是指使用setColorFilter
which 不是最初要求的。
用户@Tad的答案是正确的,但它只适用于 API 21+。
要在所有 Android 版本上设置色调,请使用ImageViewCompat
:
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(yourTint));
请注意,yourTint
在这种情况下必须是“颜色 int”。如果你有一个类似的颜色资源R.color.blue
,你需要先加载颜色 int:
ContextCompat.getColor(context, R.color.blue);
这对我有用
mImageView.setColorFilter(ContextCompat.getColor(getContext(), R.color.green_500));
@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
过多研究方法......)
借助 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);
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
简单的一行
imageView.setColorFilter(activity.getResources().getColor(R.color.your_color));
尝试这个。它应该适用于支持库支持的所有 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 的更多有趣功能。
在android中以编程方式为图像视图设置色调
我有两种适用于 android 的方法:
1)
imgView.setColorFilter(context.getResources().getColor(R.color.blue));
2)
DrawableCompat.setTint(imgView.getDrawable(),
ContextCompat.getColor(context, R.color.blue));
我希望我能帮助任何人:-)
我发现我们可以将颜色选择器用于 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>
添加到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 项目中使用的功能!
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))
}
因为第一个答案对我不起作用:
//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 来解决该问题。
我希望我能帮助任何人:-)
从 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);
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);
如果您想将选择器设置为您的色调:
ImageViewCompat.setImageTintList(iv, getResources().getColorStateList(R.color.app_icon_click_color));
不要使用PoterDuff.Mode
,使用setColorFilter()
它适用于所有人。
ImageView imageView = (ImageView) listItem.findViewById(R.id.imageView);
imageView.setColorFilter(getContext().getResources().getColor(R.color.msg_read));
正如@milosmns 所说,您应该使用
imageView.setColorFilter(getResouces().getColor(R.color.blue),android.graphics.PorterDuff.Mode.MULTIPLY);
此 API 需要颜色值而不是颜色资源 id,这就是您的语句不起作用的根本原因。
我在聚会上迟到了,但我没有在上面看到我的解决方案。我们也可以通过 设置色调颜色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)
对我来说,这段代码有效。我将它与卡片和图像视图一起使用,但我认为它可以在任何视图中更改其色调颜色。cardBookmark 是我的cardView。
var cardDrawable: Drawable = binding.cardBookmark.background
cardDrawable = DrawableCompat.wrap(cardDrawable)
DrawableCompat.setTint(cardDrawable, resources.getColor(R.color.shuffleColor))
binding.cardBookmark.background = cardDrawable
不是确切的答案,而是一个更简单的选择:
这是一个片段:
<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>