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