2012-11-09 80 views
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)。它與中心對齊,但文本不會將中間對齊到左側,而是將中間對齊。另外,在畫布上對齊文字的正確方法是什麼?

enter image description here

但我想

enter image description here

回答

5

進出口使用

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))); 

居中我的多行文字帆布

+0

這爲我工作的,謝謝。 –

+0

爲什麼不能使用pass Alignment.ALIGN_CENTER? – jjxtra

+0

我不再記得,預計它沒有工作。 – r2DoesInc