在画布上创一个黑色空间的Android掩码位图 [英] Android Mask bitmap on canvas gen a black space
问题描述
我有一个半掩码位图是红色,那些是透明的这样的
https://www.dropbox.com/s/931ixef6myzusi0/s_2.png
I have a mask bitmap with a half is red color and ones is transparent like this https://www.dropbox.com/s/931ixef6myzusi0/s_2.png
我想用掩码位图绘制在画布上只在红色区域可见内容,code是这样的:
I want to use mask bitmap to draw content on canvas only visible in red area, code like this:
涂料粉刷=新的油漆();
Paint paint = new Paint();
公共无效画(油画画布){
//这里画的内容
...
public void draw(Canvas canvas) { // draw content here ...
//和掩码位图在这里
paint.setXfermode(新PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
canvas.drawBitmap(maskBitmap,0,0,油漆);
//and mask bitmap here paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN)); canvas.drawBitmap(maskBitmap, 0, 0, paint);
}
结果我的期待(内容仅在红色区域可见,但透明区域变成黑色是课题!)
The result as my expecting (content only visible in red area, BUT THE TRANSPARENT AREA BECOME BLACK IS PROBLEM!)
这个形象的结果: https://www.dropbox.com/s/ mqj48992wllfkiq / S_2%20copy.png
谁能帮助我?
this image result :https://www.dropbox.com/s/mqj48992wllfkiq/s_2%20copy.png Anyone help me???
推荐答案
下面是解决方案,它帮助我实现屏蔽
Here is solution which helped me to implement masking
public void draw(Canvas canvas) {
Bitmap original = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.original_image);
Bitmap mask = Bitmap.createBitmap(getContext().getResources(),R.drawable.mask_image);
//You can change original image here and draw anything you want to be masked on it.
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas tempCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
tempCanvas.drawBitmap(original, 0, 0, null);
tempCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
//Draw result after performing masking
canvas.drawBitmap(result, 0, 0, new Paint());
}
面膜应与trasparency白色图像。结果
它的工作是这样的:结果
+ =
the mask should be white image with trasparency.
It will work like this:
+ =
这篇关于在画布上创一个黑色空间的Android掩码位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!