正确获取宽高方式:
private static float[] getTextBounds(Paint paint, String txt) {
Rect rect = new Rect();
float[] floats = new float[2];
paint.getTextBounds(txt, 0, txt.length(), rect);
int w = rect.width();
int h = rect.height();
floats[0] = w;
floats[1] = h;
return floats;
}
或 正确获取 宽度,如下:
paint.measureText(txt);
以下是 错误的 获取 高度 的方式:
这个错误的 方法返回的 高度 是 getTextBounds(paint,txt) 方法返回值 接近两倍,这个是错的,以此记录
public static float measureTextHeight(Paint paint) {
float height = 0f;
if (null == paint) {
return height;
}
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
height = fontMetrics.descent - fontMetrics.ascent;
return height;
}