Reputation: 32253
I'm starter with Canvas and Paint. I want to paint a text in a Canvas but it can be longer than the original Bitmap. So the text go out the Bitmap.
Is there some kind of automatic manager for this making a new line when the end is reached? or should I play with heights and distances? Thanks
Upvotes: 1
Views: 4090
Reputation: 3169
The best way is to draw text with StaticLayout:
// init StaticLayout for text
StaticLayout textLayout = new StaticLayout(
gText, paint, textWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
// get height of multiline text
int textHeight = textLayout.getHeight();
// get position of text's top left corner
float x = (bitmap.getWidth() - textWidth)/2;
float y = (bitmap.getHeight() - textHeight)/2;
// draw text to the Canvas center
canvas.save();
canvas.translate(x, y);
textLayout.draw(canvas);
canvas.restore();
See my blogpost for more details.
Upvotes: 1
Reputation: 340
I would suggest that you also look at this code snippet found here: https://stackoverflow.com/a/15092729/1759409
As it will manage the writing of your text within a certain width and height and automatically draws onto the canvas correctly.
Upvotes: 0