我使用textpaint来测量文字宽度,但它经常发生,textPaint.measureText(string)
非常慢。
如何更改代码以加快测量?
这是我的代码 -
void appendLines(String text, TextPaint textPaint){
String[] linesWords = text.split(" ", -1);
for (String linesWord : linesWords) {
appendWords( " " + linesWord , textPaint);
}
}
boolean firstWord = false;
private void appendWords(String text, TextPaint textPaint){
if (text.contains("<br/>")) {
text = text.replace("<br/>", "");
haveBrTag = true;
}
float textWidth = textPaint.measureText(text);
if(textWidth + currentLineWidth + marginRightLeft> pageWidth) {
finalizeAppend(textPaint);
firstWord = true;
}
if (currentLineWidth == 0){
text = text.replace(" ", "");
firstWord = false;
}
currentLineWidth += textWidth;
currentLine.append(text);
if (haveBrTag){
finalizeAppend(textPaint);
haveBrTag = false;
}
}