【发布时间】:2011-04-19 09:55:19
【问题描述】:
我们过去常常找到适合给定文本的矩形,例如,如果在 gettextbounds api 中给出“TESTING”,它将给出一个适合给定字符串“TESTING”的矩形,但任何人都可以澄清矩形长度的基础计算出来的,是否会考虑字体大小,我可以这样检查吗?
1) 我尝试过的方式 CharSequence 文本 = 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);
在上述两种方法中,我试图找出给定文本大小的各种矩形大小。如果这不是一个好方法,请通过发布一些示例代码来告诉我。简单地说,我必须找到适合各种字体大小的文本“TESTING”的各种矩形。
提前致谢。
【问题讨论】:
标签: android