views:

61

answers:

1

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?):

  1. 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

  2. make a mask bitmap - there are ways to blit with a separate mask?

  3. use a BitmapShader with drawArc()/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?

  4. 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?

+1  A: 

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 a DstIn or DstOut xfermode.

Romain Guy
how would you configure the matrix?
Will
I don't know, it depends on what you want to draw.
Romain Guy
aha, all I can find on google is a few people saying it costs them hours to work out how to set up the matrix, and this comes from people like me who do understand matrices, e.g. http://markmail.org/message/qjb6dxotj4jk57n4 - please can someone document it and do a few proper examples with scaling and such, Romain?
Will
The intermediate bitmap with the alpha pattern already drawn into it and then the bitmap drawn into it with SRC_IN xfer mode, before the intermediate bitmap being blitted into the scene worked.
Will