要在Android中使用drawText方法居中繪制文本,可以通過以下步驟實現:
String text = "Hello, World!";
Paint paint = new Paint();
paint.setTextSize(50);
float textWidth = paint.measureText(text);
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
float textHeight = bounds.height();
int x = (canvas.getWidth() - textWidth) / 2;
int y = (canvas.getHeight() + textHeight) / 2;
canvas.drawText(text, x, y, paint);
這樣就可以在Android中居中繪制文本了。