我在一个android应用程序上工作,我有一个可绘制的,我正在从源图像加载。在这个图像上,我想将所有的白色像素转换为不同的颜色,比如蓝色,然后缓存生成的Drawable对象,以便以后使用它。
例如,我有一个20x20的PNG文件,中间有一个白色的圆圈,圆圈之外的一切都是透明的。把白色圆圈变成蓝色并缓存结果的最好方法是什么?如果我想使用源图像来创建几个新的Drawables(比如蓝色、红色、绿色、橙色等),答案会改变吗?
我猜我想在某种程度上使用一个ColorMatrix,但我不确定如何。
我在一个android应用程序上工作,我有一个可绘制的,我正在从源图像加载。在这个图像上,我想将所有的白色像素转换为不同的颜色,比如蓝色,然后缓存生成的Drawable对象,以便以后使用它。
例如,我有一个20x20的PNG文件,中间有一个白色的圆圈,圆圈之外的一切都是透明的。把白色圆圈变成蓝色并缓存结果的最好方法是什么?如果我想使用源图像来创建几个新的Drawables(比如蓝色、红色、绿色、橙色等),答案会改变吗?
我猜我想在某种程度上使用一个ColorMatrix,但我不确定如何。
当前回答
我知道这个问题是在棒棒糖之前问的,但我想在Android 5上添加一个很好的方法来做到这一点。你创建了一个引用原始图形的xml绘图,并像这样设置着色:
<?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"/>
其他回答
根据isWorking字段更改可绘制颜色的简短示例。
我的形状xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/holo_blue_bright" />
<corners android:radius="30dp" />
<size
android:height="15dp"
android:width="15dp" />
</shape>
我的改变方法:
private Drawable getColoredDrawable(int drawableResId, boolean isworking) {
Drawable d = getResources().getDrawable(R.drawable.shape);
ColorFilter filter = new LightingColorFilter(
isworking ? Color.GREEN : Color.RED,
isworking ? Color.GREEN : Color.RED);
d.setColorFilter(filter);
return d;
}
用法示例:
text1.setCompoundDrawablesWithIntrinsicBounds(getColoredDrawable(R.drawable.shape, isworking()), null, null, null);
新的支持v4将tint带回api 4。
你可以这样做
public static Drawable setTint(Drawable d, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(d);
DrawableCompat.setTint(wrappedDrawable, color);
return wrappedDrawable;
}
Int color = Color.GRAY;
// or int color = Color.argb(123,255,0,5);
// or int color = 0xaaff000;
在XML /res/values/color.xml中
<?xml version="1.0" encoding="utf-8">
<resources>
<color name="colorRed">#ff0000</color>
</resoures>
Java代码
int color = ContextCompat.getColor(context, R.color.colorRed);
GradientDrawable drawableBg = yourView.getBackground().mutate();
drawableBg.setColor(color);
我可以用下面的代码做到这一点,这是从一个活动(布局是一个非常简单的,只包含一个ImageView,并没有张贴在这里)。
private static final int[] FROM_COLOR = new int[]{49, 179, 110};
private static final int THRESHOLD = 3;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.test_colors);
ImageView iv = (ImageView) findViewById(R.id.img);
Drawable d = getResources().getDrawable(RES);
iv.setImageDrawable(adjust(d));
}
private Drawable adjust(Drawable d)
{
int to = Color.RED;
//Need to copy to ensure that the bitmap is mutable.
Bitmap src = ((BitmapDrawable) d).getBitmap();
Bitmap bitmap = src.copy(Bitmap.Config.ARGB_8888, true);
for(int x = 0;x < bitmap.getWidth();x++)
for(int y = 0;y < bitmap.getHeight();y++)
if(match(bitmap.getPixel(x, y)))
bitmap.setPixel(x, y, to);
return new BitmapDrawable(bitmap);
}
private boolean match(int pixel)
{
//There may be a better way to match, but I wanted to do a comparison ignoring
//transparency, so I couldn't just do a direct integer compare.
return Math.abs(Color.red(pixel) - FROM_COLOR[0]) < THRESHOLD &&
Math.abs(Color.green(pixel) - FROM_COLOR[1]) < THRESHOLD &&
Math.abs(Color.blue(pixel) - FROM_COLOR[2]) < THRESHOLD;
}
如果你在ImageView中有可绘制的集合,你可以用一行代码完成:
yourImageView.setColorFilter(context.getResources().getColor(R.color.YOUR_COLOR_HERE);