2
我開發的應用程序攝影,和我使用此代碼:如何對齊文本中心,在畫布上留下的Android
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();
在這段代碼中我使用paint.setTextAlign(Align.CENTER)
。它與中心對齊,但文本不會將中間對齊到左側,而是將中間對齊。另外,在畫布上對齊文字的正確方法是什麼?
但我想
這爲我工作的,謝謝。 –
爲什麼不能使用pass Alignment.ALIGN_CENTER? – jjxtra
我不再記得,預計它沒有工作。 – r2DoesInc