问题:
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