Reputation: 2907
I am trying to draw text on a canvas, and then rotate the canvas so the text is displayed upside down. However it still does not display upside down. The bitmap is displayed upside down but the text isn't.
Canvas canvas = new Canvas(bm);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
canvas.rotate(180, bm.getWidth()/2, bm.getHeight()/2);
Upvotes: 2
Views: 1702
Reputation: 20875
When you transform a canvas, you are actually performing an update on the transformation matrix, so that subsequent painting calls will be transformed. As an example, if you want to "pad" your painting, you first translate the canvas:
canvas.translate(10.0f, 0);
and then paint a Rect on (0, 0):
canvas.drawRect(0, 0, 20, 10);
The rectangle will be draw with its origin translated to (10, 0), so that it'd be as if you had called drawRect(10, 0)
, because every 2D point is multiplied by the current transformation matrix. The same applies for other affine transformations, like rotation and scaling. So if you want to draw upside down text you have to apply the transformation first, and then painting.
By the way, to draw text actually upside down, the transformation you are looking for is not rotation, but a swap of the Y coordinate:
canvas.scale(1, -1);
canvas.drawText(text, 0, bm.getHeight()/2, paint);
Also, be sure to understand the difference between a Canvas
and a Bitmap
: the former is the Android API class used to expose the various painting APIs, the latter is an array of the actual pixels in the image (and as I recall until 2.3 it's allocated from native libraries, so never forget to call Bitmap.recycle()
or you'll soon end up the available space and get OutOfMemoryException
s.
Upvotes: 3