问题描述
有一个在画图类中的方法: Paint.getTextBounds()返回矩形
占领的一些文字。但根据这个回答它返回的TextView的不同的东西,然后宽度/高度。
There is a method in Paint class: Paint.getTextBounds() which returns Rect
occupied by some text. But according to this answer it returns something different then width/height of TextView.
Q1:有没有办法让宽度和使用矩形
TextView的高度由 Paint.getTextBounds()?
Q1: Is there a way to get width and height of TextView using Rect
returned by Paint.getTextBounds()?
请注意,我需要知道宽/高precisely。我会很高兴地知道上限的矩形
可能出现的误差大约2-3%,但必须不大于(也应该适用于任何手机不依赖于屏幕分辨率和像素密度),那么TextView的边界
Note, I do need to know width/height precisely. I will be happy to know upper bound of the rect
with possible error about 2-3%, but it MUST be not greater (and should work for any phone not depending on screen resolution and pixel density) then TextView bounds
Q2:是否有确定的宽度一些文字和高度与其他的快速方法指定TEXTSIZE
我知道,宽度可以通过 Paint.measureText()的,但是这不返回高度。身高可以通过创建新的 StaticLayout
文本,然后调用的的,但是这是太慢了。我需要更多的东西更快。
I know, width can be determined by Paint.measureText(), but this doesn't return height. Height can be determined by creating new StaticLayout
with text and then calling StaticLayout.getHeight(), but this is too slow. I need something more quicker.
的背景,这一切正在 AutoFitTextView
,将其边界内automaticaly适合文本通过向上或向下缩放文字大小,它应该迅速地做到这一点,因为会有很多这样的 AutoFitTextView
取值动态变化非常快。
The background for all of this is making AutoFitTextView
which will automaticaly fit text inside its bounds by up- or down-scaling text size, and it should do this quickly, as there will be many of such AutoFitTextView
s changed dynamically very quickly.
推荐答案
找到确定文本的宽度/高度的简单,不慢的方法绘制特定油漆
那并不是 T选用 StaticLayout
。
Found a simple and not slow method of determining text width/height drawn with specific Paint
that doesn't use StaticLayout
.
public int getTextWidth(String text, Paint paint) {
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int width = bounds.left + bounds.width();
return width;
}
public int getTextHeight(String text, Paint paint) {
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
int height = bounds.bottom + bounds.height();
return height;
}
招短期简单的说明: Paint.getTextBounds(字符串文本,诠释开始,诠释年底,矩形边界)
返回矩形
这不开始于(0,0)
。也就是说,得到的文本实际宽度,将通过调用 Canvas.drawText(字符串文本,浮动的x,浮动Y,油漆涂料)
被设置在相同从
油漆
对象getTextBounds()您应该添加矩形的左边位置
。
The short simple description of trick: Paint.getTextBounds(String text, int start, int end, Rect bounds)
returns Rect
which doesn't starts at (0,0)
. That is, to get actual width of text that will be set by calling Canvas.drawText(String text, float x, float y, Paint paint)
with the same Paint
object from getTextBounds()
you should add the left position of Rect
.
注意这个 bounds.left
- 这个问题的关键
Notice this bounds.left
- this the key of the problem.
在这种方式,您将收到的文本的宽度相同,您将获得使用 Canvas.drawText()
。
In this way you will receive the same width of text, that you would receive using Canvas.drawText()
.
更详细的解释见这个的答案。
Much more detailed explanation is given in this answer.
这篇关于如何快速获取宽度和TextView中使用Paint.getTextBounds高度()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!