Android下如何计算要显示的字符串所占的宽度和高度

Rect bounds = new Rect();
String text = "Hello World";
TextPaint paint;
paint = findViewById(R.id.hello_world).getPaint();
paint.getTextBounds(text, 0, text.length(), bounds);
int width = bounds.width();



Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("豆", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());
posted @ 2017-05-26 17:59  lucktian  阅读(3865)  评论(0编辑  收藏  举报