14

複数行テキストの段落の幅を計算しようとしています。私の知る限り、Android でこれを実行できる唯一のクラスは StaticLayout (または DynamicLayout) クラスです。このクラスを使用すると、テキスト スニペットの適切な長さが得られませんが、測定された寸法は、テキスト サイズに応じて小さい場合と大きい場合があります。

だから私は基本的に、複数行のテキスト文字列の幅を確実に測定する方法を探しています。

次の図は、さまざまなテキスト サイズで測定された幅が実際のテキストの長さとどのように異なるかを示しています。テキストとメジャーの分岐

スクリーンショットは、カスタム ビューで次のコードを実行して作成されます。

@Override
protected void onDraw( Canvas canvas ) {
  for( int i = 0; i < 15; i++ ) {
    int startSize = 10;
    int curSize = i + startSize;
    paint.setTextSize( curSize );
    String text = i + startSize + " - " + TEXT_SNIPPET;
    layout = new StaticLayout( text,
                               paint,
                               Integer.MAX_VALUE,
                               Alignment.ALIGN_NORMAL,
                               1.0f,
                               0.0f,
                               true );

    float top = STEP_DISTANCE * i;
    float measuredWidth = layout.getLineMax( 0 );
    canvas.drawRect( 0, top, measuredWidth, top + curSize, bgPaint );
    canvas.drawText( text, 0, STEP_DISTANCE * i + curSize, paint );
  }
}
4

3 に答える 3

1

測定されたテキスト幅が少しずれているという同様の問題がありましたが、書体をペイントに設定すると、これはなくなります。したがって、paintオブジェクトを使用する前にStaticLayout、書体を設定します。

textPaint.setTypeface(Typeface.create("sans-serif", Typeface.NORMAL))

またはあなたが望むどんな書体でも。

于 2015-12-01T02:55:22.507 に答える
1

任意のテキスト長のマルチライン幅を取得する方法を次に示します。usableButtonWidth特定のテキストスペースにすることができます。usableButtonWidth=ボタンの幅-パディングを使用しました。ただし、任意のデフォルト値を使用できます。

StaticLayout を使用すると、 のテキストの高さを取得できますusableButtonWidth。次に、ループで 1px ずつ減らして、使用可能な幅を減らそうとします。高さが増加すると、以前に使用された幅が最大許容値であったことがわかります。

この値をマルチ テキストの幅として返します。

private int getTextWidth(){
    if(this.getText().length() != 0){
        Paint textPaint = new Paint();
        Rect bounds = new Rect();
        textPaint.setTextSize(this.getTextSize());
        if(mTypeface != null){
            textPaint.setTypeface(mTypeface);
        }
        String buttonText = this.getText().toString();
        textPaint.getTextBounds(buttonText, 0, buttonText.length(), bounds);

        if(bounds.width() > usableButtonWidth){
            TextPaint longTextPaint = new TextPaint();
            longTextPaint.setTextSize(this.getTextSize());
            if(mTypeface != null){
                longTextPaint.setTypeface(mTypeface);
            }
            StaticLayout staticLayout = new StaticLayout(this.getText(), longTextPaint, usableButtonWidth,
                    Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            int textLineCount = (int)Math.ceil(staticLayout.getHeight() / bounds.height());
            int multiLineTextWidth = usableButtonWidth;
            while ((int)Math.ceil(staticLayout.getHeight() / bounds.height()) == textLineCount && multiLineTextWidth > 1){
                multiLineTextWidth--;
                staticLayout = new StaticLayout(this.getText(), longTextPaint, multiLineTextWidth,
                        Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            }
            return multiLineTextWidth+1;
        } else {
            return bounds.width();
        }
    } else {
        return 0;
    }
}
于 2016-09-08T11:56:50.483 に答える
1

get text bounds を使用してみてください。

private int calculateWidthFromFontSize(String testString, int currentSize)
{
    Rect bounds = new Rect();
    Paint paint = new Paint();
    paint.setTextSize(currentSize);
    paint.getTextBounds(testString, 0, testString.length(), bounds);

    return (int) Math.ceil( bounds.width());
}

private int calculateHeightFromFontSize(String testString, int currentSize)
{
    Rect bounds = new Rect();
    Paint paint = new Paint();
    paint.setTextSize(currentSize);
    paint.getTextBounds(testString, 0, testString.length(), bounds);

    return (int) Math.ceil( bounds.height());
}
于 2012-07-05T21:51:04.413 に答える