I want to make a dynamic alpha mask with drawable shapes as circles or whatever, and apply it to a drawed text on Android. Here is an example of what i want :
I am trying to make it with setXfermode(new PorterDuffXfermode(Mode.SRC_IN))
, but i can't get it work.
Here is the code I have in onDraw(Canvas canvas)
method :
Paint paint = new Paint();
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.WHITE);
canvas.drawCircle(50, 50, 50, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
paint.setColor(Color.RED);
canvas.drawText("hello", 0, 50, paint);
Thanks in advance for your help
Try creating your source and mask bitmaps separately. Most of the examples I have seen involve using two bitmaps and using drawBitmap to perform the masking.
I use PorterDuff.Mode.DST_IN for the paint, then draw the source image (with no paint) followed by the mask image (with the paint). Something like this:
Bitmap bitmapOut = Bitmap.createBitmap(sizeX, sizeY,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapOut);
Paint xferPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
xferPaint.setColor(Color.BLACK);
xferPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
canvas.drawBitmap(sourceImage, 0, 0, null);
canvas.drawBitmap(alphaMask, 0, 0, xferPaint);
At this point, bitmapOut contains my masked image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With