Android:Paint.getTextBounds返回的宽度远大于屏幕的宽度

Android:Paint.getTextBounds返回的宽度远大于屏幕的宽度,android,paint,Android,Paint,我有一个文本视图,它在根布局下的宽度为fill\u parent,这意味着它的屏幕宽度为480 我试图在文本视图中测量此文本的边界。所以我这样做了: Rect rect = new Rect(); textView.getPaint().getTextBounds(text,0,text.length()-1,rect); Log.v(TAG,"Width : Height ="+rect.width()+" : "+width.height()); 但它打印的是10278:79。为什么当sc

我有一个文本视图,它在根布局下的宽度为
fill\u parent
,这意味着它的屏幕宽度为
480

我试图在文本视图中测量此文本的边界。所以我这样做了:

Rect rect = new Rect();
textView.getPaint().getTextBounds(text,0,text.length()-1,rect);
Log.v(TAG,"Width : Height ="+rect.width()+" : "+width.height());
但它打印的是
10278:79
。为什么当scren宽度本身为
480
时,它会将宽度打印为10278?如果它假设它是一条单行并进行计算,那么使用
getTextBounds
有什么意义呢

不返回视图的大小,但返回显示所给的完整字符串所需的最小空间:

在边界内返回(由调用方分配)包含所有字符的最小矩形,其隐含原点为(0,0)


这意味着,对于1000个字符,并且给定
Paint
对象中当前配置的字体,打印它们至少需要10278像素。您的文本或绘制对象是否来自textView并不重要。

文档中指出,int end是“要测量的字符串中最后一个字符的1”。,int,int,android.graphics.Rect)

你应该:

textView.getPaint().getTextBounds(text, 0, text.length(), rect);
这也适用于Canvas.drawText()

显示文本的TextView将“包装”文本。getTextBounds方法根本不知道TextView

事实上,如果要构建自己的显示文本的自定义视图,getTextBounds对于实现自己的文本包装非常有用。下面的代码在表示方面做得很糟糕,但是对字符串进行了迭代,将其部分绘制到画布上,有效地包装了文本

    private void drawText(Canvas canvas) {

        String text = "Nunc eleifend erat eu nulla varius iaculis. Quisque feugiat justo sit amet" +
                " neque interdum tempor. Curabitur faucibus facilisis tristique. Donec posuere" +
                " viverra magna, eu accumsan sapien congue id. Cras fringilla justo ut lacus" +
                " molestie, et venenatis orci egestas. Vivamus pretium, nisl quis cursus cursus," +
                " dolor eros porta neque, sit amet viverra ante orci non elit. Donec odio neque," +
                " volutpat sit amet dui sit amet, fringilla tempus sapien. Donec sed mollis justo." +
                " In dignissim tincidunt neque, eu luctus justo egestas nec. Donec commodo ut arcu" +
                " vel placerat. Donec ullamcorper justo eget justo commodo hendrerit. Quisque" +
                " commodo imperdiet posuere. Aenean vehicula dui.";

        Paint paint = new Paint();
        paint.setTextSize(30);
        paint.setAntiAlias(true);

        int padding = 20;

        int availWidth = getWidth() - 2 * padding;
        int availHeight = getHeight() - 2 * padding;

        Rect bounds = new Rect();
        int currentTextBottom = 0;
        int firstCharInLine = 0;
        int lastCharInLine = 0;

        outerLoop: while (currentTextBottom < availHeight) {

            while (Character.isWhitespace(text.charAt(firstCharInLine))) {
                lastCharInLine = ++firstCharInLine;
            }

            for (int i = firstCharInLine + 1; i < text.length(); i++) {
                paint.getTextBounds(text, firstCharInLine, i, bounds);

                if (bounds.width() <= availWidth) {
                    if (Character.isWhitespace(text.charAt(i))) {
                        lastCharInLine = i;
                    }
                } else {
                    currentTextBottom += bounds.height();
                    if (firstCharInLine == lastCharInLine) {
                        lastCharInLine = i;
                    }
                    canvas.drawText(text, firstCharInLine, lastCharInLine, padding, currentTextBottom + padding, paint);
                    firstCharInLine = lastCharInLine++;
                    break;
                }
                if (i == text.length() - 1) {
                    currentTextBottom += bounds.height();
                    lastCharInLine = text.length();
                    canvas.drawText(text, firstCharInLine, lastCharInLine, padding, currentTextBottom + padding, paint);
                    break outerLoop;
                }
            }
        }
    }
}
但这不能正确地水平对齐。当我这样做的时候:

float textWidth = paint.measureText(text);
canvas.drawText(
        text,
        viewWidth / 2f - textWidth / 2,
        viewHeight / 2f + bounds.height() / 2f,
        paint);

它符合我的要求:)

你能发布完整的代码吗?不清楚
text
中的值来自何处。@Sebastian:text是字符串。一根长绳子。包含大约1000个字符。@Sebastin-那为什么要有高度属性呢。那么你的意思是假设它作为一个单行文本来计算宽度?如果是这样的话,它与measureText()方法有何不同?getTextBounds返回最小的矩形(不仅是宽度),因此还将获得高度。如果在文本中包含换行符,您可以进行实验以了解会发生什么。Re:measureText,检查@Sebastin:好的。所以只有当文本中有一个新行字符时,它才会进入下一行?而不是当宽度超过文本视图宽度时?我现在不能确定它对换行符的作用,但正如我所说的,您可以尝试使用不同的字符串来自己找到答案。它肯定不会考虑屏幕的大小。”如果(bounds.width()否,则为字符串的一个不断增加的部分计算边界(从firstCharInLine到但不包括i)。在某个点上,它将超过availWidth,然后您将换行
float textWidth = paint.measureText(text);
canvas.drawText(
        text,
        viewWidth / 2f - textWidth / 2,
        viewHeight / 2f + bounds.height() / 2f,
        paint);