- Paint设置
//设置画笔宽度
paint.setStrokeWidth(5);
//指定抗锯齿功能
paint.setAntiAlias(true);
//绘图样式
paint.setStyle(Paint.Style.FILL);
//设置文字对齐方式
paint.setTextAlign(Align.CENTER);
//设置文字大小
paint.setTextSize(12);
//粗体文字
paint.setFakeBoldText(true);
//设置下划线
paint.setUnderlineText(true);
//设置字体水平倾斜度
paint.setTextSkewX((float)-0.25);
//设置带有删除线效果
paint.setStrikeThruText(true);
//水平方向拉伸
paint.setTextScaleX(2);
- Canvas文字 :
//普通绘制,指定绘制起点(x,y)
void drawText(String text, float x, flozt y,Paint paint)
//通过指定字符串的起始位置和终止位置进行绘制,他匀速我们指定CharSequence或者String类型字符串进行绘制, start表示字符串开始的下标,end表示终止下标,x,y表示起始点坐标
void drawText(CharSequence text, int start, int end, float x, float y, Paint paint)
void drawText(String text, int start, int end, dfloat x, float y, Paint paint)
- 逐个指定文字位置:
void drawPosText(String text, float[] pos, Paint paint)
void drawPosText(char[] text, int index, int count, float[] pos, Paint paint)
@Override
protected void onDraw(Canvas canvas){
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(80);
float[]pos = new float[]{80,100,
80,200,
80,300,
80,400,
80,500}; //截取部分左闭右开
canvas.drawPosText("王拣贤佩奇",pos, paint);
}
- 沿路径绘制:
void drawTextonPath(String text,Path path, float vOffset, Paint paint)
void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset, float vOffset, Paint paint)
* float hOffset:与路径起始点的水平偏移量
* float vOffset:与路径中心的垂直偏移量
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(45);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
Path circlePath = new Path();
circlePath.addCircle(220, 300, 150, Path.Direction.CCW);
canvas.drawPath(circlePath, paint);
Path circlePath2 = new Path();
circlePath2.addCircle(600, 300, 150, Path.Direction.CCW);
canvas.drawPath(circlePath2, paint);
String string = "王拣贤佩奇王拣贤佩奇王拣贤佩奇";
canvas.drawTextOnPath(string, circlePath, 0,0,paint);
canvas.drawTextOnPath(string, circlePath2, 80, 30,paint);
- 设置字体样式:
Typeface setTypeface(Typeface typeface)
Typeface是专门用来设置字体样式的类,通过paint.setTypeface()函数来指定即将绘制的文字的字体样式。可以指定系统中的字体样式,也可以在自定义的样式文件中获取,在构建Typeface类时,可以指定所用样式的正常体,斜体,粗体等,如果载指定样式中没有相关文字的样式,就会用默认的样式来显示,一般默认为宋体。
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTypeface(Typeface.SERIF);
paint.setTextSize(50);
- defaultFromStyle()函数:
Typeface defaultFromStyle(int style);
int style取值如下 :
Typeface.NORMAL //正常字体
Typeface.BOLD //粗体
Typeface.ITALIC //斜体
Typeface.BOLD_ITALIC //粗斜体
Paint paint = new Paint();
Typeface typeface = Typeface.defaultFromStyle(Typeface.BOLD_ITALIC);
paint.setColor(Color.BLUE);
paint.setTypeface(typeface);
paint.setTextSize(50);
- create(String familyName, int style)
Typeface create(String familyName, int style)
该函数直接指定字体名来加载系统自带的字体样式,如果字体样式不存在,则会用系统样式来代替并返回。
Paint paint = new Paint();paint.setColor(Color.BLUE);
paint.setTextSize(50);
String string = "宋体";
Typeface font = Typeface.create(string, Typeface.NORMAL);
paint.setTypeface(font);
canvas.drawText("王拣贤佩奇",10, 100, paint);