简体   繁体   English

如何使用Paint.getTextBounds()快速获取TextView的宽度和高度?

[英]How to quickly get width and height of TextView using Paint.getTextBounds()?

There is a method in Paint class: Paint.getTextBounds() which returns Rect occupied by some text. Paint类中有一个方法: Paint.getTextBounds()返回一些文本占用的Rect But according to this answer it returns something different then width/height of TextView. 但根据这个答案,它会返回与TextView的宽度/高度不同的东西。

Q1: Is there a way to get width and height of TextView using Rect returned by Paint.getTextBounds() ? Q1:有没有办法使用Paint.getTextBounds()返回的Rect获取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 我很乐意知道rect上限可能有大约2-3%的误差,但它必须不大(并且应该适用于任何不依赖于屏幕分辨率和像素密度的手机)然后TextView界限

Q2: Is there any other QUICK method of determining width and height of some text with specified textSize? Q2:有没有其他QUICK方法用指定的textSize确定某些文本的宽度和高度?

I know, width can be determined by Paint.measureText() , but this doesn't return height. 我知道,宽度可以由Paint.measureText()确定,但这不会返回高度。 Height can be determined by creating new StaticLayout with text and then calling StaticLayout.getHeight() , but this is too slow. 可以通过使用文本创建新的StaticLayout然后调用StaticLayout.getHeight()来确定高度,但这太慢了。 I need something more quicker. 我需要更快的东西。


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. 所有这一切的背景是制作AutoFitTextView ,它将通过向上或向下缩放文本大小自动将文本放入其边界内,并且它应该快速完成,因为很多这样的AutoFitTextView很快动态变化。

Found a simple and not slow method of determining text width/height drawn with specific Paint that doesn't use StaticLayout . 找到一种简单而不慢的方法来确定使用不使用StaticLayout特定Paint绘制的文本宽度/高度。

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;
}

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) . 技巧的简短描述: Paint.getTextBounds(String text, int start, int end, Rect bounds)返回不从(0,0)开始的Rect 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 . 也就是说,要获得文本的实际宽度将通过调用设置Canvas.drawText(String text, float x, float y, Paint paint)使用相同的 Paint从对象getTextBounds()你应该添加的左侧位置Rect

Notice this bounds.left - this the key of the problem. 注意这个bounds.left - 这是问题的关键。

In this way you will receive the same width of text, that you would receive using Canvas.drawText() . 通过这种方式,您将获得使用Canvas.drawText()接收的相同宽度的文本。


Much more detailed explanation is given in this answer. 这个答案中给出了更详细的解释。

With java I used class FontMetrics. 使用java我使用了类FontMetrics。

Font f= new Font("Arial", Font.BOLD, 16);
FontMetrics metric = getFontMetrics(f);


metric.stringWidth("the text I want to measure");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM