Android Paint.breakText是否包含最大宽度处的字符?
用于绘制.breakText的 breakTextAndroid Paint.breakText是否包含最大宽度处的字符?,android,paint,Android,Paint,用于绘制.breakText的 breakText int breakText (CharSequence text, int start, int end, boolean measureForwards, float maxWidth, float[] measuredWidth) 测量文本,如果测量的宽度超过 最大宽
int breakText (CharSequence text,
int start,
int end,
boolean measureForwards,
float maxWidth,
float[] measuredWidth)
测量文本,如果测量的宽度超过
最大宽度。返回已测量的字符数,如果
measuredWidth不为空,请在其中返回实际测量的宽度
返回值int
测量的字符数。将始终是否,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
String text=“Hello World”;
油漆=新油漆();
油漆.尺寸(100);
//分别测量子串
int length=text.length();
对于(int i=1;i