Android自定义View时获取文字宽高

来源:互联网 发布:网络安全产品分类 编辑:程序博客网 时间:2024/06/15 00:58

获取文字宽度:

private int getTextWidth(String text, Paint paint) {    Rect rect = new Rect(); // 文字所在区域的矩形    paint.getTextBounds(text, 0, text.length(), rect);    return rect.width();}

获取文字高度:

private int getTextHeight(String text, Paint paint) {    Rect rect = new Rect();    paint.getTextBounds(text, 0, text.length(), rect);    return rect.height();}
原创粉丝点击