自定义view中获取字符串的宽度跟高度

自定义view中获取字符串的宽度和高度
方法1:
Rect rect = new Rect();
Paint p = new Paint();
String str = "abcdefg";
p.getTextBounds(str, 0, str.length(), rect);//用一个矩形去"套"字符串,获得能完全套住字符串的最小矩形
float width = rect.width();//字符串的宽度
float height = rect.height();//字符串的高度


方法2:
Paint p = new Paint();
String str = "abcdefg";
float width = p.measureText(str);//获得字符串的宽度