1. 如何获取字符串的长度
方法一:
Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTextPaint.setColor(Color.WHITE); // Define the string. String displayText = “Hello World!”; // 直接返回参数字符串所占用的宽度 float textWidth = mTextPaint.measureText(displayText);
方法二:
Paint pFont = new Paint();
Rect rect = new Rect();
//返回包围整个字符串的最小的一个Rect区域
pFont.getTextBounds(str, 0, 1, rect);
strwid = rect.width();
strhei = rect.height();
注意:
记得设字体的大小哦,不然取出的大小可能跟实际需要有偏差~