Monday, 26 August 2013

Android drawText including text wrapping

Android drawText including text wrapping

I am currently creating an image editor and am attempting to draw text on
top of on image using canvas.drawText(). So far I have been successful in
doing this but when the user enters text that is too long, the text just
continues on one line out of the page and doesn't wrap itself to the width
of the screen. How would I go about doing this? I have tried using a
static layout but cannot seem to get it to work, has anyone got a tutorial
to do this?
My function for drawing on a canvas using static layout:
public Bitmap createImage(float scr_x,float scr_y,String user_text){
Canvas canvas = new Canvas(image);
scr_x = 100;
scr_y = 100;
final TextPaint tp = new TextPaint(Color.WHITE);
canvas.save();
StaticLayout sl = new StaticLayout("" + user_text, tp,
originalBitmap.getWidth(), Layout.Alignment.ALIGN_NORMAL,
1.0f, 0.0f, false);
sl.draw(canvas);
return image;
}

No comments:

Post a Comment