Android ApiDemos樣本解析(68):Graphics->MeasureText

來源:互聯網
上載者:User

Canvas提供drawText,drawPostText在螢幕上顯示文字,字型的類型和大小是通過設定paint 的屬性來定義的。

Paint同時也提供了使用當前字型和大小繪製文字串時,文字在螢幕上佔據的大小(寬度,高度,範圍等)。

MeasureText 介紹了如何使用Paint提供的方法來測量文字的大小。

[java] 
mPaint.setTextSize(64); 
mPaint.setTypeface(Typeface.create(Typeface.SERIF, 
 Typeface.ITALIC)); 
  
... 
int count = mPaint.getTextWidths(text, 0, text.length(), widths); 
float w = mPaint.measureText(text, 0, text.length()); 
mPaint.getTextBounds(text, 0, text.length(), bounds); 

mPaint.setTextSize(64);
mPaint.setTypeface(Typeface.create(Typeface.SERIF,
 Typeface.ITALIC));
 
...
int count = mPaint.getTextWidths(text, 0, text.length(), widths);
float w = mPaint.measureText(text, 0, text.length());
mPaint.getTextBounds(text, 0, text.length(), bounds);文字最終的大小是和繪製文字的字型的類型和字型的大小是相關的,字型的類型和大小都是通過Paint對象來設定的setTypeface,setTextSize。

getTextWidths 可以提供widths數組返回text字串中對應的每個字元使用當前字型繪製的寬度。而measureText則返回整個字串的寬度。getTextBounds則是返回字串佔據的矩形地區大小。

 
作者:mapdigit
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.