How can you blit a non-rectangular (e.g. oval) part of a bitmap into a canvas on Android?
Consider how you’d blit a rectangular part of a bitmap:
canvas.DrawBitmap(src,src_rect,dest_rect,paint). Sadly there is no corresponding methods for non-rectangular regions.
Four approaches present themselves (maybe you know a fifth?):
-
copy the rectangular bounds you want to blit into an intermediate bitmap, and go setting the pixels you don’t want to blit to be transparent, then draw that bitmap
-
make a mask bitmap – there are ways to blit with a separate mask?
-
use a
BitmapShaderwithdrawArc()/drawCircle(); however, I can’t work out how to get the matrix to be properly aligned; how would you initialize the matrix for this operation? -
use a very very complicated clipping region
Of these, option 3 is the one that I would most like to work; however, I cannot work out how to do so; can you?
You can use option number #3, it’s probably the easiest. Another way is to draw the shape you want to clip with in an intermediate Bitmap (
ARGB8888), then draw your original Bitmap using aDstInorDstOutxfermode.