`
George_ghc
  • 浏览: 91289 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android 中如何得到字符的像素宽度

阅读更多
Android 中如何得到字符串的像素宽度。?
条件是:在不同字体,和不同大小的情况下都可以
字符串宽度可以这样:
Paint paint = new Paint();
float strWidth = paint.measureText(String);
paint.setTextSize(tv.getTextSize());
二.
private final Paint pFont = new Paint();
Rect rect = new Rect();
pFont.getTextBounds("小", 0, 1, rect);
Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());

Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);   
mTextPaint.setColor(Color.WHITE);   
// Define the string.   
String displayText = “Hello World!”;   
// Measure the width of the text string.   
float textWidth = mTextPaint.measureText(displayText);  


Paint mPaint = new Paint();  
mPaint.setTextSize(16);  
float FontSpace = mPaint.getFontSpacing();
return text.length()*FontSpace;
最后的结果 用字符串长度 × 字体宽度。得到字符长度最接近实际长度,但是当出现英文或者是标点符号时,长度就会缩小
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics