3

Java2Dクラスを aおよび an とTextLayout一緒に使用して、テキストをボックスに描画しています。テキストが折り返されます。LineBreakMeasurerAttributedCharacterIterator

プロファイリングは、コードが非常に遅いことを示しています。ほとんどの時間はメソッドで失われTextLayout.draw(..)ます。

速度を改善するための提案はありますか?

    // Get iterator for string
    AttributedCharacterIterator iterator = attribText.getIterator();

    // Create measurer
    LineBreakMeasurer measurer = new LineBreakMeasurer(iterator, context);

    // loop over the lines
    int i = 1;
    while (measurer.getPosition() < iterator.getEndIndex()) {
        // Get line
        TextLayout textLayout = measurer.nextLayout(w);

        // get measurements
        float ascent  = textLayout.getAscent();
        float descent = textLayout.getDescent();
        float leading = textLayout.getLeading();
        float size    = ascent + descent;

        // Move down to baseline
        if( i == 1 ) {
            if( coverType == CoverType.SPINE ) {
                y = (box.height-size)/2;
                y -= (size+leading)*(lines-1)/2;
            } else if( vAlign == Alignment.Center ) {
                y += (h-size)/2-(size+leading)*(lines-1)/2;
            } else if( vAlign == Alignment.Bottom ) {
                y += (h-size) - (size+leading)*(lines-1);
            }
        }
        y += ascent;

        // calculate starting point for alignment
        float paintX = x;
        switch( hAlign ) {
            case Right: {
                paintX = x + w - textLayout.getVisibleAdvance();
                break;
            }
            case Center: {
                paintX = x + (w - textLayout.getVisibleAdvance())/2;
                break;
            }
        }

        // Draw line
        textLayout.draw(g2d, paintX, y);

        // Move down to top of next line
        y += descent + leading;
        i++;
    }

関連するコード スニペットを上に示します。attribText前のAttributtedStringセットです。contextですg2d.getFontRenderContext()

4

1 に答える 1

0

この投稿はかなり古くなっているので、ニーズに合った解決策を見つけていただければ幸いです。あなたがここにいないのなら、考えるべきことがあります。表示領域内にあるテキストを描画するだけで済みます。各線のy座標がわかっているので、yがgetVisibleRect()の範囲内にあるかどうかを簡単に確認できます。必要なテキストのみをペイントすると、パフォーマンスが大幅に向上します(もちろん、テキストが1ページより長いと仮定します)。

于 2011-01-04T04:39:34.680 に答える