Android 以编程方式更改可绘制颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11376516/
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 07:12:37  来源:igfitidea点击:

Change drawable color programmatically

android

提问by Johan

I'm trying to change the color on a white marker image by code. I have read that the code below should change the color, but my marker remains white.

我正在尝试通过代码更改白色标记图像上的颜色。我已经读到下面的代码应该改变颜色,但我的标记仍然是白色的。

Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )

Did I miss something? Is there any other way to change colors on my drawables located in my res folder?

我错过了什么?有没有其他方法可以更改位于 res 文件夹中的可绘制对象的颜色?

回答by ρяσ?ρ?я K

Try this:

尝试这个:

Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable); 
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);    

Using DrawableCompatis important because it provides backwards compatibility and bug fixes on API 22 devices and earlier.

使用DrawableCompat很重要,因为它提供了 API 22 设备及更早版本的向后兼容性和错误修复。

回答by amorenew

You can try this for svg vector drawable

你可以试试这个用于 svg vector drawable

DrawableCompat.setTint(
    DrawableCompat.wrap(myImageView.getDrawable()),
    ContextCompat.getColor(context, R.color.another_nice_color)
);

回答by shicky

You may need to call mutate() on the drawable or else all icons are affected.

您可能需要在 drawable 上调用 mutate() 否则所有图标都会受到影响。

Drawable icon = ContextCompat.getDrawable(getContext(), R.drawable.ic_my_icon).mutate();
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(R.attr.colorIcon, typedValue, true);
icon.setColorFilter(typedValue.data, PorterDuff.Mode.SRC_ATOP);

回答by MinceMan

Another way to do this on Lollipop, android 5.+ is setting a tint on a bitmap drawable like such:

在 Lollipop 上执行此操作的另一种方法,android 5.+ 是在可绘制的位图上设置色调,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/ic_back"
    android:tint="@color/red_tint"/>

This will work for you if you have a limited number of colors you want to use on your drawables. Check out my blog post for more information.

如果您想在可绘制对象上使用的颜色数量有限,这将对您有用。查看我的博客文章了解更多信息

回答by Jaydip Meghapara

You can try this for ImageView. using setColorFilter().

你可以试试这个ImageView。使用setColorFilter().

imageViewIcon.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite));

回答by Sachin Tanpure

I have wrote a generic function in which you can pass context, icon is id drawable/mipmap image icon and new color which you need for that icon.

我编写了一个通用函数,您可以在其中传递上下文,图标是 id drawable/mipmap 图像图标和该图标所需的新颜色。

This function returns a drawable.

这个函数返回一个drawable。

public static Drawable changeDrawableColor(Context context,int icon, int newColor) {
    Drawable mDrawable = ContextCompat.getDrawable(context, icon).mutate(); 
    mDrawable.setColorFilter(new PorterDuffColorFilter(newColor, PorterDuff.Mode.SRC_IN)); 
    return mDrawable;
} 

changeDrawableColor(getContext(),R.mipmap.ic_action_tune, Color.WHITE);

回答by tiguchi

You could try a ColorMatrixColorFilter, since your key color is white:

您可以尝试使用 ColorMatrixColorFilter,因为您的主要颜色是白色:

// Assuming "color" is your target color
float r = Color.red(color) / 255f;
float g = Color.green(color) / 255f;
float b = Color.blue(color) / 255f;

ColorMatrix cm = new ColorMatrix(new float[] {
        // Change red channel
        r, 0, 0, 0, 0,
        // Change green channel
        0, g, 0, 0, 0,
        // Change blue channel
        0, 0, b, 0, 0,
        // Keep alpha channel
        0, 0, 0, 1, 0,
});
ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm);
myDrawable.setColorFilter(cf);

回答by Bek

This worked for me. Make sure to put "ff" between 0x and color code. Like this 0xff2196F3

这对我有用。确保在 0x 和颜色代码之间放置“ff”。像这样 0xff2196F3

Drawable mDrawable = ContextCompat.getDrawable(MainActivity.this,R.drawable.ic_vector_home);
                    mDrawable.setColorFilter(new
                            PorterDuffColorFilter(0xff2196F3,PorterDuff.Mode.SRC_IN));

回答by CorayThan

Same as the accepted answer but a simpler convenience method:

与接受的答案相同,但更简单的便捷方法:

val myDrawable = ContextCompat.getDrawable(context, R.drawable.my_drawable)
myDrawable.setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_IN)
setCompoundDrawablesWithIntrinsicBounds(myDrawable, null, null, null)

Note, the code here is Kotlin.

注意,这里的代码是 Kotlin。

回答by sebsebmc

You may want to try Mode.LIGHTENor Mode.DARKEN. The Android Javadocs are horrible at explaining what the PorterDuff Modes do. You can take a look at them here: PorterDuff | Android

您可能想尝试Mode.LIGHTENMode.DARKEN。Android Javadocs 在解释 PorterDuff 模式的作用方面非常糟糕。您可以在此处查看它们:PorterDuff | 安卓

I suggest looking around at Compositing on Mozilla's site here.(They don't have all the modes that android does but they have a lot of them)

我建议在此处查看Mozilla 网站上的合成(他们没有 android 的所有模式,但他们有很多模式)