首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >测量要在画布上绘制的文本宽度( Android )

测量要在画布上绘制的文本宽度( Android )
EN

Stack Overflow用户
提问于 2010-07-15 23:45:53
回答 7查看 79.9K关注 0票数 135

有没有一种方法可以根据绘制文本时使用的drawText()方法返回要在Android canvas上绘制的文本的宽度(以像素为单位)?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2010-07-16 00:02:40

您看过android.graphics.Paint#measureText(String txt)(http://developer.android.com/reference/android/graphics/Paint.html#measureText(java.lang.String%29)?

票数 244
EN

Stack Overflow用户

发布于 2014-11-17 22:54:10

代码语言:javascript
运行
复制
Paint paint = new Paint();
Rect bounds = new Rect();

int text_height = 0;
int text_width = 0;

paint.setTypeface(Typeface.DEFAULT);// your preference here
paint.setTextSize(25);// have this the same as your text size

String text = "Some random text";

paint.getTextBounds(text, 0, text.length(), bounds);

text_height =  bounds.height();
text_width =  bounds.width();
票数 35
EN

Stack Overflow用户

发布于 2017-02-02 15:05:26

补充答案

Paint.measureTextPaint.getTextBounds返回的宽度略有不同。measureText返回一个宽度,其中包含字形的advanceX值,填充字符串的开头和结尾。getTextBounds返回的Rect宽度没有这种填充,因为边界是将文本紧密换行的Rect

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3257293

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档