绘制系列(五)-DrawText()详解
文字基础知识
1、setTextAlign()
设置文字对齐方式(起始点相对于文字)
canvas.drawText(“文字”,起始点,paint)
比如: paint.setTextAlign(Paint.Align.RIGHT)//表示起始点在文字的右侧开始绘制;
###2、设置字体样式 ###
- 设置粗体 setBoldText(boolean fakeBoldText)
- 设置下划线 setUnderLineText(boolean underLineText)
- 设置是否显示中间删除线 setStrikeThruText()
- 设置字体倾斜度 setTextSkewX(float skewX),取负值向右倾斜,取正值向左倾斜;
- 水平拉伸 setTextScaleX(floate scaleX)取值为1,表示不拉伸
- 设置字体seTypeface(Typeface typeface);Typeface是字体样式的类,可以指定系统中的字体,也可以在自定义样式文件中获取
字体样式
- Android中自带的字体样式,Typeface类中保存着三种自带字体样式,Typeface。SAN_SERIF、Typeface.MONOSPACE、Typeface.SERIF,这三种的字体对中文支持不很有友好,当遇到不支持的文字时,会使用系统默认的样式来写;
-Typeface create(String familyName,int style)
通过指定字体名来加载系统中自带的字体样式,如果字体样式不存在,则会用紫铜样式代替并返回;
一般情况下,我们会选择加载自定义的字体文件来绘制文字,而不是指定系统自带的字体样式;
要自定义字体样式:
-
从外部字体文件中加载所需字形
Typeface createFromAsset(AssetManager mgr,String apth) createFromFile(String path) createFromFile(File path)
绘制文字
- public void drawText(String text, float x, float y,Paint paint)
- public void drawText(String text, int start, int end, float x, float y,Paint paint)
- public void drawText(CharSequence text, int start, int end, float x, float y,Paint paint)
- public void drawText(char[] text, int index, int count, float x, float y,Paint paint)
- public void drawTextOnPath(char[] text, int