問題:
I'm trying to display a text using the code below.我正在嘗試使用下面的代碼顯示文本。 The problem is that the text is not centered horizontally.問題是文本沒有水平居中。 When I set the coordinates for drawText
, it sets the bottom of the text at this position.當我爲drawText
設置座標時,它會在此位置設置文本的底部。 I would like the text to be drawn so that the text is centered also horizontally.我希望繪製文本,以便文本也水平居中。
This is a picture to display my problem further:這是進一步顯示我的問題的圖片:
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
//canvas.drawRGB(2, 2, 200);
Paint textPaint = new Paint();
textPaint.setARGB(200, 254, 0, 0);
textPaint.setTextAlign(Align.CENTER);
textPaint.setTypeface(font);
textPaint.setTextSize(300);
canvas.drawText("Hello", canvas.getWidth()/2, canvas.getHeight()/2 , textPaint);
}
解決方案:
參考一: https://en.stackoom.com/question/kevA參考二: https://stackoom.com/question/kevA