以cx
和为中心的示例cy
:
private final Rect textBounds = new Rect(); //don't new this up in a draw methodpublic void drawTextCentred(Canvas canvas, Paint paint, String text, float cx, float cy){ paint.getTextBounds(text, 0, text.length(), textBounds); canvas.drawText(text, cx - textBounds.exactCenterX(), cy - textBounds.exactCenterY(), paint);}为什么不height()/2f
一样?
exactCentre()=
(top + bottom) / 2f。
height()/2f=
(bottom - top) / 2f
当
top是时,这些只会产生相同的结果
0。对于所有尺寸的某些字体或某些尺寸的其他字体,可能会是这种情况,但对于所有尺寸的所有字体却并非如此。



