画布上的 Android 中心文本 - Android Center text on canvas

问题:

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章