Android中的Gettextbounds

Pra*_*ath 13 android

我们曾经找到适合给定文本的矩形,例如,如果在gettextbounds api中给出"TESTING",它将给出一个适合给定字符串"TESTING"的矩形,但是任何plz都可以澄清在哪个基础上计算矩形长度,我是否可以这样检查字体大小是否可以考虑?

1)我试过CharSequence text = getText(); canvas.drawText(text,0,text.length(),mTextX,mTextY,getPaint());

    Paint pt = new Paint ( );
    pt.setTextSize(10);

    TextPaint tp = getPaint();
    String string = "haa";
    Rect currentBounds = new Rect ( );
    //this.setTextSize(/* TypedValue.COMPLEX_UNIT_PX */ 10, /* fontPixelSize*Home.fltFontRatio */ 32);

    tp.getTextBounds((String) text, 0, text.length(), currentBounds );

    Log.e ( " ", "Desired Text " +text);
    Log.e ( " ", "first Ondraw Left " +currentBounds.left);
    Log.e ( " ", "Ondraw Top" +currentBounds.top);
    Log.e ( " ", "Ondraw right " +currentBounds.right);
    Log.e ( " ", "Ondraw bottom " +currentBounds.bottom);

    pt.setTextSize(20);


    tp.getTextBounds((String) text, 0, text.length(), currentBounds );

    Log.e ( "", "Desired Text " +text);
    Log.e ( " ", "Second Ondraw Left " +currentBounds.left);
    Log.e ( " ", "Ondraw Top" +currentBounds.top);
    Log.e ( " ", "Ondraw right " +currentBounds.right);
    Log.e ( "Nrace ", "Ondraw bottom " +currentBounds.bottom);
Run Code Online (Sandbox Code Playgroud)

2)我尝试的第二种方式

    TextPaint tp = getPaint();
    String string = "haa";
    Rect currentBounds = new Rect ( );
    this.setTextSize(/* TypedValue.COMPLEX_UNIT_PX */ 10, /* fontPixelSize*Home.fltFontRatio */ 32);               

    tp.getTextBounds(string, 0, string.length(), currentBounds );

    Log.e ( " ", "first Left " +currentBounds.left);
    Log.e ( " ", "Top" +currentBounds.top);
    Log.e ( " ", "right " +currentBounds.right);
    Log.e ( " ", "bottom " +currentBounds.bottom);

    this.setTextSize(/* TypedValue.COMPLEX_UNIT_PX */ 10, /* fontPixelSize*Home.fltFontRatio */ 10);

    tp.getTextBounds(string, 0, string.length(), currentBounds );

    Log.e ( " ", "Sefond Left " +currentBounds.left);
    Log.e ( " ", "Top" +currentBounds.top);
    Log.e ( " ", "right " +currentBounds.right);
    Log.e ( "", "bottom " +currentBounds.bottom);
Run Code Online (Sandbox Code Playgroud)

在上面两种方法中,我试图找出给定文本大小的各种矩形大小.如果这不是一个好方法,请通过发布一些示例代码来告诉我.简单地说,我必须找到适合各种字体大小的文本"TESTING"的各种矩形.

提前致谢.

dam*_*911 47

我发现API的这一部分相当混乱,但我想我几乎理解它是如何工作的.对getTextBounds()的调用返回最小的矩形,该矩形将包含随后调用drawText()的x = 0和y = 0所绘制的所有字符.这在API参考中以略微不同的词语表示.Paint中的所有内容都可能会影响文本的外观.这是一个例子:

Rect bounds = new Rect();

Paint paint = new Paint();
paint.setTextAlign(Align.LEFT);
paint.setTypeface(typeface);
paint.setTextSize(textSize);
paint.getTextBounds(text, 0, text.length(), bounds);
Run Code Online (Sandbox Code Playgroud)

矩形具有这种异域坐标的原因是因为当您使用drawText()绘制文本时,x和y相对的原点取决于您选择的Paint的印刷属性.例如,y是相对于基线的,它既不高于也不低于字体,但通常在中间的某处撞击它.因此,边界矩形将具有(通常)负顶部和正底部.负顶部意味着文本的顶部高于基线(y减小上升),而正底部意味着文本的底部低于基线(y增加下降).有趣的是,当您测量诸如"Hi"之类的字符串时,底部可能为0,因为这些字符没有低于基线部分.相反,当你测量像"Hg"这样的字符串时,你很可能得到正底,因为g 的故事低于基线.不过,我不确定如何估算水平位置.

有了这个说法,要绘制你已经计算了边界的文本,你可以做:

canvas.drawText(text, -bounds.left, -bounds.top, paint);
Run Code Online (Sandbox Code Playgroud)

这将绘制靠近画布左上角的文本.左上角的坐标是(0,0),因此要移动文本,只需添加所需的位移量:

canvas.drawText(text, -bounds.left + yourX, -bounds.top + yourY, paint);
Run Code Online (Sandbox Code Playgroud)

另一个例子:如果你想创建一个包含文本的位图,并且你想要完全适合可用空间,你可以这样做:

Bitmap bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, -bounds.left, -bounds.top, paint);
Run Code Online (Sandbox Code Playgroud)

如果你想在左边,右边,顶部和底部留下几个像素,让我们说2,你可以这样做:

Bitmap bitmap = Bitmap.createBitmap(bounds.width() + 4, bounds.height() + 4, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, -bounds.left + 2, -bounds.top + 2, paint);
Run Code Online (Sandbox Code Playgroud)

  • 我几次尝试过这个,我很确定答案是正确的.它对你有用吗?如果是这种情况,也许你可以接受答案,它可能会帮助处于相同情况的其他人. (2认同)
  • 这可能是http://stackoverflow.com/questions/7549182/android-paint-measuretext-vs-gettextbounds?lq=1中提到的问题,但问题是关于**getTextBounds**,特别是*澄清哪个计算矩形长度的基础,是否考虑字体大小,如果可以,我可以像这样检查*. (2认同)