两种计算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;
}

 

posted @ 2017-08-15 21:59  Charles04  阅读(191)  评论(0编辑  收藏  举报