0

I am using

pCanvas.drawBitmap(mBitmap, mRectSrc, mRectDst, mPainter);

to draw a subset of a bitmap. I wondering how I can rotate that bitmap without affecting the view. In my attempts, when I set the canvas to rotate the whole view (viewport) is rotated. This is not what I want.

Nanne
  • 64,065
  • 16
  • 119
  • 163
KITT
  • 230
  • 2
  • 3
  • 15

2 Answers2

1

when I had to draw rotated text, I found the procedure is to call Canvas.save(), rotate (remembering that the center point remains the same), do the drawing and then call Canvas.restore(). I suppose it's just the same in this case.

bigstones
  • 15,087
  • 7
  • 65
  • 82
  • Actually, I don't have to use save/restore. – KITT Mar 11 '11 at 06:29
  • @user445299 well that's if you have to draw a bitmap and *then* rotate the whole canvas. If you need to do some "normal" drawing and then draw a rotated bitmap, you first have to save, rotate the canvas, draw as if it were "normal" and then restore. – bigstones Mar 11 '11 at 15:10
0

Only way I know of is to use a Matrix. Try this pseudocode:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.your_bitmap);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
user432209
  • 20,007
  • 10
  • 56
  • 75
  • Here's what's working for me (minus the skewed rectangles): 1. Convert int src and dst Rects to s, r RectFs (float) 2. Matrix m = new Matrix(); 3. m.postRotate(...); 4. m.mapRect(r, s) 5. Bitmap rotatedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, w, h, m, true); 6...//other steps 7. canvas.drawBitmap(rotatedBitmap, SrcRect, r, painter); – KITT Mar 11 '11 at 06:31