要在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中居中绘制文本了。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:Android中drawtext怎么使用