【发布时间】:2015-09-22 07:33:32
【问题描述】:
因为我需要我的drawable 作为位图描述符,所以我将我的资源转换为Drawable,然后转换为Bitmap。
但我想改变它的颜色,所以我使用了以下代码:
private static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
int iColor = Color.parseColor("#006f00");
drawable.setColorFilter(iColor, PorterDuff.Mode.SRC_ATOP);
drawable.draw(canvas);
return bitmap;
}
但是drawable的颜色仍然是黑色。 你能告诉我为什么以及如何让它工作吗?
【问题讨论】: