计算文本的宽度
/** * 方法1:获取文本的宽度 * @param paint * @param str * @return */ public static int getTextWidth(Paint paint, String str) { int iRet = 0; if (str != null && str.length() > 0) { int len = str.length(); float[] widths = new float[len]; paint.getTextWidths(str, widths); for (int j = 0; j < len; j++) { iRet += (int) Math.ceil(widths[j]); } } return iRet; }
//方法2 int textWidth = (int) mTextPaint.measureText(mText, 0, mText.length());