Reputation: 3293
I'm attempting to create a mask by drawing lines on a canvas. However, when I finish with the image it's just of the original image. If I send the mask
bitmap it shows as expected and the original image is correct. How can I get the masking to work between the two bitmaps?
@Override
public void onDraw(Canvas canvas) {
if (lines == null) {
return;
}
Bitmap mask = null;
if(performMask) {
mask = Bitmap.createBitmap((int)getWidth(), (int)getHeight(), Bitmap.Config.RGB_565);
canvas = new Canvas(mask);
}
for (int i = 0; i < lines.size(); i++) {
ArrayList<Point> line = lines.get(i);
for (int j = 1; j < line.size(); j++) {
Point start = line.get(j - 1);
Point end = line.get(j);
canvas.drawLine(start.x, start.y, end.x, end.y, drawingPaint);
}
}
if(performMask) {
performMask = false;
Bitmap result = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.ARGB_8888);
Canvas tempCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
tempCanvas.drawBitmap(original, 0, 0, null);
tempCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
if(maskPerformedListener != null) {
maskPerformedListener.onMaskCompleted(result);
}
}
}
Upvotes: 0
Views: 1598
Reputation: 3293
I needed to change this line
mask = Bitmap.createBitmap((int)getWidth(), (int)getHeight(), Bitmap.Config.RGB_565);
to
mask = Bitmap.createBitmap((int)getWidth(), (int)getHeight(), Bitmap.Config.RGB_8888);
Turns out you need the alpha layer for this to work.
Upvotes: 2