VR.Naresh
VR.Naresh

Reputation: 299

How to align text center to left on canvas Android

I am developing Photography apps, and for that I am using this code:

Canvas canvas = new Canvas(bmOverlay);
TextPaint paint = new TextPaint();
paint.setColor(Color.RED);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(50);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
// if the background image is defined in main.xml, omit this line
canvas.drawBitmap(mBitmap, 0, 0, null);
int left = measureTextWidth(paint, InstaTextActivity.CurrentWord);
int top = measureTextHeight(paint, InstaTextActivity.CurrentWord);

left = mBitmap.getWidth() / 2 - InstaTextActivity.textCount / 2;
top = mBitmap.getHeight() / 2 - InstaTextActivity.textCount / 2;
StaticLayout layout = new StaticLayout(InstaTextActivity.CurrentWord, paint, total,
 android.text.Layout.Alignment.ALIGN_NORMAL, (float) 1.0, (float) 0.0, true);
//canvas.save();
canvas.translate(left, top);
layout.draw(canvas);
//canvas.restore();

In this code I am using paint.setTextAlign(Align.CENTER). It aligns to centre, but text doesn't align center to left, instead it aligns centre to right. Also, what is the proper way to align text on canvas?

enter image description here

But I want

enter image description here

Upvotes: 3

Views: 5094

Answers (1)

r2DoesInc
r2DoesInc

Reputation: 3771

Im using

StaticLayout mTextLayout = new StaticLayout(text, mTextPaint,
                canvas.getWidth(), Alignment.ALIGN_NORMAL, 1.0f, 0.0f,
                false);
canvas.translate((canvas.getWidth() / 2) - (mTextLayout.getWidth() / 2), (canvas.getHeight() / 2) - ((mTextLayout.getHeight() / 2)));

to center my multi line text on a canvas

Upvotes: 6

Related Questions