Addev
Addev

Reputation: 32221

Paint text in a Canvas in Android

I want to paint text inside a Canvas (auto adding the new lines when needed). Thats my sample code:

    Bitmap src= getBitmap();
    Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas cs = new Canvas(dest);
    cs.drawBitmap(src, 0f, 0f, null);
    TextPaint tp= new TextPaint();
    tp.setTextSize(.....);
    tp. //Custom the text properties
    StaticLayout sl= new StaticLayout(text, tp, src.getWidth(), Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false);
    sl.draw(cs);

this works fine for painting the text with "center|top" gravity. But I'd need to be able to make "center|center" and "center|bottom". Given the canvas src and sl is easy to calculate where sl must go but how can I change the "start point" for the StaticLayout in order to add it a padding?

Thanks

Upvotes: 1

Views: 2909

Answers (1)

Andreas
Andreas

Reputation: 1617

You can give the StaticLayout/DynamicLayout some more space in the "width" arguement, to get some padding on top/bottom of your text you can use "canvas.translate(x,y)"

Upvotes: 2

Related Questions