android drawBitmap with PorterDuff.Mode.DST_IN ,应该透明的区域变成黑色
android drawBitmap with PorterDuff.Mode.DST_IN ,the area which should be transparent turn out to be black
我正在尝试绘制带有特定形状遮罩的图像。
我正在使用 Facebook 的图书馆 Fresco,这里是代码:
ImageRequest request = builder.setPostprocessor(new BasePostprocessor() {
@Override
public String getName() {
return "ChatViewImageProcessor";
}
@Override
public void process(Bitmap bitmap) {
int bitmap_width = bitmap.getWidth();
int bitmap_height = bitmap.getHeight();
ViewGroup.LayoutParams params = iv.getLayoutParams();
if (width == -1 && height == -1) {//not relavant with question
horizontal[0] = bitmap_width >= bitmap_height;
params.width = horizontal[0] ? max_image_size : (int) (max_image_size * 1.0 * bitmap_width / bitmap_height);
params.height = (horizontal[0] ? (int) (max_image_size * 1.0 * bitmap_height / bitmap_width) : max_image_size);
}
Bitmap temp = Bitmap.createBitmap(params.width, params.height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp);
if (bg != null) {
bg.setBounds(0, 0, params.width, params.height);
bg.draw(canvas);
}
Logger.out("temp[0,0]:" + Util.getColorNote(temp.getPixel(0, 0), true));
canvas = new Canvas(bitmap);
Rect src = new Rect(0, 0, temp.getWidth(), temp.getHeight());
Rect dst = new Rect(0, 0, bitmap_width, bitmap_height);
canvas.drawBitmap(temp, src, dst, paint);//<----------problem here
temp.recycle();
Logger.out("result[0,0]:" + Util.getColorNote(bitmap.getPixel(0, 0), true));
}
}).build();
PipelineDraweeController controller = (PipelineDraweeController)
Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(iv.getController())
.build();
iv.setController(controller);
此代码在 5.0.2 上运行良好...
我无法在此处 post 图片,请参阅 http://i.stack.imgur.com/9DBbj.jpg
但在 4.4.4 和 4.1.2 上失败。
我无法在此处 post 图片,请参阅 http://i.stack.imgur.com/8wbLp.jpg
我还在 5.0.2 上打印了 "temp" 左上角像素的颜色并裁剪了 "bitmap",即 #00000000 和 #00000000,在 4.4.4 和 4.1.2 上它们是#00000000 和#FF000000
我很高兴在 Fresco 的 github 问题页面上找到答案 here
正如它所说,我只需要调用
bitmap.setHasAlpha(true);
在
public void process(Bitmap bitmap)
我正在尝试绘制带有特定形状遮罩的图像。 我正在使用 Facebook 的图书馆 Fresco,这里是代码:
ImageRequest request = builder.setPostprocessor(new BasePostprocessor() {
@Override
public String getName() {
return "ChatViewImageProcessor";
}
@Override
public void process(Bitmap bitmap) {
int bitmap_width = bitmap.getWidth();
int bitmap_height = bitmap.getHeight();
ViewGroup.LayoutParams params = iv.getLayoutParams();
if (width == -1 && height == -1) {//not relavant with question
horizontal[0] = bitmap_width >= bitmap_height;
params.width = horizontal[0] ? max_image_size : (int) (max_image_size * 1.0 * bitmap_width / bitmap_height);
params.height = (horizontal[0] ? (int) (max_image_size * 1.0 * bitmap_height / bitmap_width) : max_image_size);
}
Bitmap temp = Bitmap.createBitmap(params.width, params.height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(temp);
if (bg != null) {
bg.setBounds(0, 0, params.width, params.height);
bg.draw(canvas);
}
Logger.out("temp[0,0]:" + Util.getColorNote(temp.getPixel(0, 0), true));
canvas = new Canvas(bitmap);
Rect src = new Rect(0, 0, temp.getWidth(), temp.getHeight());
Rect dst = new Rect(0, 0, bitmap_width, bitmap_height);
canvas.drawBitmap(temp, src, dst, paint);//<----------problem here
temp.recycle();
Logger.out("result[0,0]:" + Util.getColorNote(bitmap.getPixel(0, 0), true));
}
}).build();
PipelineDraweeController controller = (PipelineDraweeController)
Fresco.newDraweeControllerBuilder()
.setImageRequest(request)
.setOldController(iv.getController())
.build();
iv.setController(controller);
此代码在 5.0.2 上运行良好... 我无法在此处 post 图片,请参阅 http://i.stack.imgur.com/9DBbj.jpg
但在 4.4.4 和 4.1.2 上失败。 我无法在此处 post 图片,请参阅 http://i.stack.imgur.com/8wbLp.jpg
我还在 5.0.2 上打印了 "temp" 左上角像素的颜色并裁剪了 "bitmap",即 #00000000 和 #00000000,在 4.4.4 和 4.1.2 上它们是#00000000 和#FF000000
我很高兴在 Fresco 的 github 问题页面上找到答案 here
正如它所说,我只需要调用
bitmap.setHasAlpha(true);
在
public void process(Bitmap bitmap)