我正在尝试通过代码更改白色标记图像上的颜色。我读过下面的代码应该更改颜色,但是我的标记仍然是白色。
Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )
我错过了什么?还有其他方法可以更改res文件夹中可绘制对象的颜色吗?
我正在尝试通过代码更改白色标记图像上的颜色。我读过下面的代码应该更改颜色,但是我的标记仍然是白色。
Drawable.setColorFilter( 0xffff0000, Mode.MULTIPLY )
我错过了什么?还有其他方法可以更改res文件夹中可绘制对象的颜色吗?
Answers:
试试这个:
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, R.drawable.my_drawable);
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, Color.RED);
使用DrawableCompat非常重要,因为它提供了向后兼容性以及API 22及更早版本设备上的错误修复。
OverlayItems可能导致问题的mapview 类有关吗?它是从我的res文件夹中定期绘制的,没什么特别的……
您可以尝试此SVG矢量可绘制
DrawableCompat.setTint(
DrawableCompat.wrap(myImageView.getDrawable()),
ContextCompat.getColor(context, R.color.another_nice_color)
);
null给Drawable#setTintList(ColorStateList)
您可能需要在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);
在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"/>
如果您想在可绘制对象上使用的颜色数量有限,这将为您工作。查看我的博客文章以获取更多信息。
minSdkVersion 16Android 4.1.1设备进行测试。)
android:background="..."。真奇怪!
您可以尝试使用ImageView。使用setColorFilter()。
imageViewIcon.setColorFilter(ContextCompat.getColor(context, R.color.colorWhite));
我写了一个通用函数,您可以在其中传递上下文,图标是id drawable / mipmap图像图标,以及该图标所需的新颜色。
该函数返回一个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);
您可以尝试使用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);
这对我有用。确保在0x和颜色代码之间放置“ ff”。赞这个0xff2196F3
Drawable mDrawable = ContextCompat.getDrawable(MainActivity.this,R.drawable.ic_vector_home);
mDrawable.setColorFilter(new
PorterDuffColorFilter(0xff2196F3,PorterDuff.Mode.SRC_IN));
您可能要尝试Mode.LIGHTEN或Mode.DARKEN。Android Javadocs很难解释PorterDuff模式的功能。您可以在这里查看它们:PorterDuff | 安卓
我建议在这里浏览 Mozilla网站上的Compositing 。(它们没有android具备的所有模式,但是它们有很多)
句法
"your image name".setColorFilter("your context".getResources().getColor("color name"));
例
myImage.setColorFilter(mContext.getResources().getColor(R.color.deep_blue_new));
这就是我所做的:
public static Drawable changeDrawableColor(int drawableRes, int colorRes, Context context) {
//Convert drawable res to bitmap
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableRes);
final Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth() - 1, bitmap.getHeight() - 1);
final Paint p = new Paint();
final Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, p);
//Create new drawable based on bitmap
final Drawable drawable = new BitmapDrawable(context.getResources(), resultBitmap);
drawable.setColorFilter(new
PorterDuffColorFilter(context.getResources().getColor(colorRes), PorterDuff.Mode.MULTIPLY));
return drawable;
}
只需使用
android:drawableTint="@color/primary_color"
在您的XML文件中。将primary_color替换为自定义颜色
像这样创建方法:
//CHANGE ICON COLOR
private void changeIconColor(Context context ,int drawable){
Drawable unwrappedDrawable = AppCompatResources.getDrawable(context, drawable);
assert unwrappedDrawable != null;
Drawable wrappedDrawable = DrawableCompat.wrap(unwrappedDrawable);
DrawableCompat.setTint(wrappedDrawable, getResources().getColor(R.color.colorAccent));
}
并像这样使用它:
changeIconColor(this,R.drawable.ic_home);
最简单的方法:
imageView.setColorFilter(Color.rgb(r, g b));
要么
imageView.setColorFilter(Color.argb(a, r, g, b));
a,r,g,b:颜色argb值。