在安卓Gettextbounds [英] Gettextbounds in android

查看:142
本文介绍了在安卓Gettextbounds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们用来查找符合给定的文本,例如矩形如果gettextbounds给予测试的API,它会给适合给定的字符串测试的矩形,而是可以任意PLZ澄清基础矩形长度上计算,字体大小是否会被认为是如果是这样有可能是我检查这个样子?

We used to find the rectangle which fits the given text, for example if give "TESTING" in gettextbounds api it will give a rectangle that fits the given string "TESTING", but could any plz clarify on which basis the rectangle length is calculated, whether the font size will be considered if so is it possible for me to check like this ?

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

1) Way i tried 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);

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);

在上面的两种方法我试图找出给定的文本大小不同的矩形的大小。如果这不是一个好办法PLZ通过发布一些样本C $ CS $告诉我。简单地说我一定要找到它适合文本测试的各种字体大小不同的矩形。

In the above two methods am trying to find out the various rectangle size for the given text size. if this is not a good way plz advise me by posting some sample codes. Simply to say i have to find the various rectangle which fits the text "TESTING" for various font size.

在此先感谢。

推荐答案

我觉得这部分的API相当混乱,不过,我想我几乎了解它是如何工作的。调用到getTextBounds()返回,将包围()的后续调用吸引到的drawText所有字符,其中x = 0和y = 0的最小矩形。这是在对<略微不同的词语表示href="http://developer.android.com/reference/android/graphics/Paint.html#getTextBounds%28java.lang.String,%20int,%20int,%20android.graphics.Rect%29">API参考。一切是在油漆和可能影响文本的外观被考虑在内。这里有一个例子:

I find this part of the API quite confusing, however I think I almost understood how it works. A call to getTextBounds() returns the smallest rectangle that would enclose all the characters drawn by a subsequent call to drawText() with x=0 and y=0. This is stated in slightly different words on the API reference. Everything that is in Paint and could affect the appearance of the text is taken into account. Here's an example:

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);

之所以长方形有这样的异国情调的的坐标是因为当你绘制文本的drawText(),原点X和Y是相对取决于油漆您选择的印刷性能。例如y是相对于基线,这既不以上也不字体如下,但是通常触击某处横过中间。由于这个原因,边界矩形将具有(通常)的负顶部和正底部。负顶部意味着文本的顶部是以上的基线(γ减小上升),而阳性底部意味着文本的底部是以下的基线(Y增加下去)。有趣的是,要注意的是,当你测量一个字符串,如您好的底部很可能是0,因为这些字符不具有的基线以下的一部分。相反,当你测量,如汞的字符串,你很可能得到一个积极的底部,因为故事的的的g是低于基准线。我不知道怎么的水平位置估计,虽然。

The reason why the rectangle has such exotic coordinates is because when you draw text with drawText(), the origin to which x and y are relative depends on the typographical properties of the Paint that you selected. For instance y is relative to the baseline, which is neither above nor below the font, but usually strikes it somewhere across the middle. For this reason the bounds rectangle will have (usually) a negative top and a positive bottom. The negative top means that the top of the text is above the baseline (y decreases going up), while the positive bottom means that the bottom of the text is below the baseline (y increases going down). It is interesting to note that when you measure a string such as "Hi" the bottom is likely to be 0, because those characters do not have a below the baseline part. On the contrary, when you measure a string like "Hg" you are likely to get a positive bottom, because the story of the g is below the baseline. I'm not sure how the horizontal position is estimated, though.

与此说,吸取其中已计算,你可以做的范围的文字:

With this said, to draw the text of which you have computed the bounds you can do:

canvas.drawText(text, -bounds.left, -bounds.top, paint);

这将吸引近到画布的左上角的文本。左上角的坐标为(0,0),所以移动的文字环绕你只需要添加位移所需的量:

This will draw the text near to the upper left corner of the canvas. The coordinates of the upper left corner are (0, 0) so to move the text around you only have to add your desired amount of displacement:

canvas.drawText(text, -bounds.left + yourX, -bounds.top + yourY, paint);

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

Another example: if you want to create a bitmap that contains text, and you want to fit exactly the available space, you can do:

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);

如果你要离开几个像素的左,右,上,下​​,假设2,你可以这样做:

If you want to leave a few pixels to the left, right, top and bottom, let's say 2, you can do:

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);

这篇关于在安卓Gettextbounds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆