用paint 计算字符串的像素宽度

1.只得到字符宽度

//直接返回参数字符串所占用的像素宽度

1 Paint paint = new Paint();
2 width = paint.measureText(str);

有一些view可以直接得到paint,如textview,

1     TextPaint paint = noticeView.getPaint();
2     String text = ""+noticeView.getText();
3     int w = (int) paint.measureText(text); 

 

2.得到字符矩形区域大小

1 Paint paint = new Paint(); 
2 Rect rect = new Rect();
3 
4 //返回包围整个字符串的最小的一个Rect区域
5 paint.getTextBounds(str, 0, 1, rect); 
6 
7 strwid = rect.width();
8 strhei = rect.height();



posted @ 2015-12-12 21:46  f9q  阅读(251)  评论(0编辑  收藏  举报