textPaint测量文本宽度android低速

时间:2017-05-16 01:48:53

标签: android performance text width

我使用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;
            }

        }

0 个答案:

没有答案