gpt4 book ai didi

android - Paint.breakText 是否包含 maxWidth 处的字符?

转载 作者:行者123 更新时间:2023-11-29 19:11:13 26 4
gpt4 key购买 nike

documentation对于 Paint.breakText

breakText

int breakText (CharSequence text,  
int start,
int end,
boolean measureForwards,
float maxWidth,
float[] measuredWidth)

Measure the text, stopping early if the measured width exceeds maxWidth. Return the number of chars that were measured, and if measuredWidth is not null, return in it the actual width measured.

Returns
int The number of chars that were measured. Will always be <= abs(end - start).

不清楚返回的字符数是否包括超过maxWidth 的字符数因为该字符可能是在确定总测量宽度超过 maxWidth 之前测量的.

也就是说,如果我的红线代表maxWidth , 它会包括 oWorld在下图中?

enter image description here

我在下面回答我的问题作为 self 回答。

最佳答案

不,breakText 不包含使其超过maxWidth 的字符。

我们可以通过以下代码看到这一点。

String text = "Hello World";
Paint paint = new Paint();
paint.setTextSize(100);

// Measure the substrings individually
int length = text.length();
for (int i = 1; i <= length; i++) {
float totalWidth = paint.measureText(text, 0, i);
Log.i("TAG", i + ", totalWidth of " + text.substring(0, i) + ": " + totalWidth);
}

// compare these to breakText
float[] measuredWidth = new float[1];
float maxWidth = 360; // halfway through the "o" of "World"
int countedChars = paint.breakText(text, 0, length, true, maxWidth, measuredWidth);
Log.i("TAG", "countedChars: " + countedChars + " (\"" + text.substring(0, countedChars) + "\")");
Log.i("TAG", "measuredWidth: " + measuredWidth[0]);

// 1, totalWidth of H: 70.0
// 2, totalWidth of He: 123.0
// 3, totalWidth of Hel: 148.0
// 4, totalWidth of Hell: 173.0
// 5, totalWidth of Hello: 230.0
// 6, totalWidth of Hello : 255.0
// 7, totalWidth of Hello W: 344.0
// 8, totalWidth of Hello Wo: 399.0
// 9, totalWidth of Hello Wor: 433.0
//10, totalWidth of Hello Worl: 458.0
//11, totalWidth of Hello World: 515.0

// countedChars: 7 ("Hello W")
// measuredWidth: 342.0

enter image description here

为什么 measureText 给出的值 (344) 与 breakText (342) 给出的值略有不同是另一个问题。我猜这可能与 Wo 之间的字距调整有关。

关于android - Paint.breakText 是否包含 maxWidth 处的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45253574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com