首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

Android ApiDemos示范解析(68):Graphics->MeasureText

2012-07-31 
Android ApiDemos示例解析(68):Graphics-MeasureTextCanvas提供drawText,drawPostText在屏幕上显示文字,

Android ApiDemos示例解析(68):Graphics->MeasureText

Canvas提供drawText,drawPostText在屏幕上显示文字,字体的类型和大小是通过设置paint 的属性来定义的。

Paint同时也提供了使用当前字体和大小绘制文字串时,文字在屏幕上占据的大小(宽度,高度,范围等)。

MeasureText 介绍了如何使用Paint提供的方法来测量文字的大小。

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则是返回字符串占据的矩形区域大小。

Android ApiDemos示范解析(68):Graphics->MeasureText

 

热点排行