0
votes

I need some advice regarding efficient use of canvas and matrices

  • I have a source bitmap "B0" loaded in memory, which is WxH
  • I have a bitmap B1, onto which I draw with a canvas.
  • There is a rotated (with an arbitrary angle) rectangular portion (w_p*h_p) of B0. I need to get this portion and draw it, once unrotated, onto B1

I would like to do it with normal Views, canvas and matrices. Not surfaceviews, not opengl

An "already working" approach is:

  • Rotate the bitmap B0 to compensate for the selected zone rotation --> we get B0_r
  • Calculate the translated rectangle, which will now be unrotated. We have SrcRect_u
  • With a canvas, draw the selected rectangle of B0_r (srcRect_u) onto B1

However, if B0 is large enough, the rotation operation is very expensive since it applies to the whole bitmap. Also, it means to create an intermediate bitmap each time.

I need to repeat this step in a gameloop, where this rectangle (srcRect) can be rotated and translated, so it must be a "cheap" operation to achieve this

My question: is there a better approach in terms of efficiency , using canvas, matrices and "normal" Views?

EDIT To better illustrate what I mean, I have added some pics.

B0, with the rotated selection zone

Original Bitmap with highlighted rotated portion

B0 rotated. Now the selection zone is unrotated enter image description here

unrotated selection zone drawn onto a part of B1 enter image description here

1

1 Answers

0
votes

Yes. Rotate the canvas you're going to be drawing to via canvas.rotate. Then draw the subset of the bitmap you wish via drawCanvas. This will have the effect of drawing a rotated bitmap, without rotating the bitmap or creating an intermediate. If you want to draw some things rotated and some unrotated, save the matrix (via canvas.save), rotate it, draw the bitmap, then restore (via canvas.restore)