我们曾经找到适合给定文本的矩形,例如,如果在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);
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);
在上面两种方法中,我试图找出给定文本大小的各种矩形大小。如果这不是一个好方法,请通过发布一些示例代码来告诉我。简单地说,我必须找到适合各种字体大小的文本“TESTING”的各种矩形。
提前致谢。
答案 0 :(得分:43)
我发现API的这一部分相当混乱,但我想我几乎理解它是如何工作的。对getTextBounds()的调用返回最小的矩形,该矩形将包含随后调用drawText()的x = 0和y = 0所绘制的所有字符。这在API reference上以略微不同的词语表示。 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);
矩形具有外来坐标的原因是因为当您使用drawText()绘制文本时,x和y相对的原点取决于您选择的Paint的印刷属性。例如,y是相对于基线的,它既不高于也不低于字体,但通常在中间的某处撞击它。因此,边界矩形将具有(通常)负顶部和正底部。负顶部意味着文本顶部高于基线(y减小上升),而正底部意味着文本底部低于基线(y增加下降)。有趣的是,当您测量诸如“Hi”之类的字符串时,底部可能为0,因为这些字符在基线部分之下没有。相反,当您测量像“Hg”这样的字符串时,您可能会获得正底部,因为g的故事低于基线。不过,我不确定如何估算水平位置。
如上所述,要绘制已计算边界的文本,您可以这样做:
canvas.drawText(text, -bounds.left, -bounds.top, paint);
这将绘制靠近画布左上角的文本。左上角的坐标是(0,0)所以要移动你周围的文字只需要添加你想要的位移量:
canvas.drawText(text, -bounds.left + yourX, -bounds.top + yourY, paint);
另一个例子:如果你想创建一个包含文本的位图,并且你想要完全适合可用空间,你可以这样做:
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,您可以这样做:
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);
答案 1 :(得分:3)
有一些细微差别,所以最好再看一次...... 红色矩形显示文本边界,x,y - 任何坐标(文本边界的左下角)
Rect textBounds = new Rect();
Paint textPaint = new Paint();
Paint rectPaint = new Paint();
String text = "java m";
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
textPaint.setTextSize(100);
textPaint.getTextBounds(text, 0, text.length(), textBounds);
canvas.drawText(text, x, y, textPaint);
rectPaint.setColor(Color.RED);
rectPaint.setStyle(Paint.Style.STROKE);
canvas.drawRect(
x, // left
y - textBounds.height(), // top
x + textBounds.width(), // right
y, // bottom
rectPaint);
}
text =" Hello Worlds"
text =" java m"
答案 2 :(得分:2)
我的解决方案:
class Pos {
public int X;
public int Y;
}
public static void getTextPositionAndRealBound(Paint paint, String text, int x, int y, Pos pos, Rect bound) {
paint.getTextBounds(text, 0, text.length(), bound);
// in X
bound.left = x;
bound.right = (int)(paint.measureText(text) + 0.5); // .5f to 1
// in Y
y = y - bound.top;
bound.top = y;
bound.bottom = bound.height();
// reset the position
pos.X = x;
pos.Y = y;
}
你知道X是正确的,并且Y在由getTextBounds绑定的coord中向下。
解决方案可以获得文本的位置和实际界限。您可以将文本移动到任何位置,只需编辑var-pos并在调用Canvas.drawText时使用它:
Pos pos;
Rect bound;
getTextPositionAndRealBound(paint, text, x, y, pos, bound);
canvas.drawText(text, pos.X, pos.Y, paint);
注意:感谢@ToolmakerSteve建议
答案 3 :(得分:1)
这是一种适合我的方式:使用Paint.measureText(text)
获取text
的宽度,使用Paint.getTextBounds(text, 0, text.length(), bounds)
获取text
的身高(height = bounds.height()
)