歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發經驗之獲取畫在畫布上的字符串長度、寬度(所占像素寬度)

Android開發經驗之獲取畫在畫布上的字符串長度、寬度(所占像素寬度)

日期:2017/3/1 10:27:40   编辑:Linux編程

Android中獲取字符串長度、寬度(所占像素寬度) 計算出當前繪制出來的字符串有多寬,可以這麼來!

方法1:

  1. Paint paint = new Paint();
  2. Rect rect = new Rect();
  3. //返回包圍整個字符串的最小的一個Rect區域
  4. paint.getTextBounds(text, 0, 1, rect);
  5. strwid = rect.width();
  6. strhei = rect.height();

方法2:

  1. //直接返回參數字符串所占用的寬度
  2. strWidth = paintHead.measureText(text);
Copyright © Linux教程網 All Rights Reserved