分享

Android 计算字符串在屏幕上所占的长度和宽度(像素单位)

 點點滴滴 2017-06-28

获取字符串占的屏幕的宽度:

[java] view plain copy
  1. Paint mPaint = new Paint();  
  2. int width = mPaint.measureText(hello);  //在屏幕中所占的宽度 


计算出当前绘制出来的字符串有多宽,可以这么来!
方法1:

[java] view plain copy
  1. Paint pFont = new Paint();  
  2. Rect rect = new Rect();  
  3.   
  4. //返回包围整个字符串的最小的一个Rect区域  
  5. pFont.getTextBounds(str, 01, rect);  
  6.   
  7. strwid = rect.width();  
  8. strhei = rect.height();



方法2:

[java] view plain copy
  1. Rect bounds = new Rect();  
  2. String text = "Hello World";  
  3. TextPaint paint;  
  4.   
  5. paint = findViewById(R.id.hello_world).getPaint();  
  6. paint.getTextBounds(text, 0, text.length(), bounds);  
  7. int width = bounds.width();  

方法3:

[java] view plain copy
  1. Paint pFont = new Paint();  
  2. Rect rect = new Rect();  
  3. pFont.getTextBounds("豆"01, rect);  
  4. Log.v(TAG, "height:"+rect.height()+"width:"+rect.width());  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多