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?
在这段代码中,我使用的是paint.setTextAlign(Align.CENTER)。它与中心对齐,但文本不会从中心向左对齐,而是从中心向右对齐。另外,在画布上对齐文本的正确方法是什么?
But I want
但我想要
1 个解决方案
#1
6
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
将我的多行文字放在画布上
#1
6
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
将我的多行文字放在画布上