两种计算Text文本的尺寸的区别

(1)

通过getTextBounds的方式获取Text的绘制尺寸
/***
* 通过getTextBounds的方式获取Text的绘制尺寸
* @param text
* @return
*/
private int[] getTextMeasurementByBound(String text){
Rect textRect = new Rect();
Paint selectedItemPaint = new Paint();
selectedItemPaint.getTextBounds(text, 0, text.length(), textRect);
int measureHeight = textRect.height();
int measureWidth = textRect.width();
return new int[]{measureHeight, measureWidth};
}

(2)通过Layout测量的方式获取Text的绘制尺寸

/***
* 通过Layout测量的方式获取尺寸
* @param text
* @return
*/
private int getTextMeasurementByLayout(String text){
Rect textRect = new Rect();
Paint selectedItemPaint = new TextPaint();
int measureHeight = Layout.getDesiredWidth(text, selectedItemPaint);
return measureHeight;
}
原文地址:https://www.cnblogs.com/charles04/p/7368027.html