Android获取字符串宽度(字符串像素)
Android获取字符串宽度
Paint paint = new Paint();
Rect rect = new Rect();
String str = "hello";
// 方法1
// 返回包围整个字符串的Rect区域 该方法可获取宽度和高度
paint.getTextBounds(str, 0, str.length() , rect);
int width = rect.width();
int height = rect.height();
// 方法2:
// 直接返回参数字符串所占用的宽度
width = paint.measureText(str);
官方api文档
https://developer.android.google.cn/reference/android/graphics/Paint#measureText(java.lang.String)
浙公网安备 33010602011771号