1、Paint的基本实用方法和技巧
(1)基本的使用
1.1 负责图形绘制相关
//重置
mPaint.reset();
mPaint.setColor(Color.RED);
mPaint.setAlpha(255);
//设置画笔的样式
mPaint.setStyle(Paint.Style.FILL);//填充内容
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStyle(Paint.Style.STROKE);//描边
//画笔的宽度
mPaint.setStrokeWidth(50);
//线帽
mPaint.setStrokeCap(Paint.Cap.BUTT);//没有
mPaint.setStrokeCap(Paint.Cap.ROUND);//圆形
mPaint.setStrokeCap(Paint.Cap.SQUARE);//方形
mPaint.setStrokeJoin(Paint.Join.MITER);//锐角
mPaint.setStrokeJoin(Paint.Join.ROUND);//圆弧
mPaint.setStrokeJoin(Paint.Join.BEVEL);//直线
//线段的连接处的样式
mPaint.setStrokeJoin(Paint.Join.MITER);//锐角
mPaint.setStrokeJoin(Paint.Join.ROUND);//圆弧
mPaint.setStrokeJoin(Paint.Join.BEVEL);//直线
防锯齿,会损失一定的性能
mPaint.setAntiAlias(true);
1.2 负责文字绘制相关
//获得字符行间距
mPaint.getFontSpacing();
//获得字符之间的间距
mPaint.getLetterSpacing();
mPaint.setLetterSpacing(letterSpacing)//设置
//设置文本删除线
mPaint.setStrikeThruText(true);
//是否设置下划线
mPaint.setUnderlineText(true);
//设置文本大小
mPaint.setTextSize(textSize);
mPaint.getTextSize();
mPaint.setTypeface(Typeface.BOLD);//设置字体类型
Typeface.ITALIC
Typeface.create(familyName, style)//加载自定义字体
//文字倾斜 默认0,官方推荐的-0.25f是斜体
mPaint.setTextSkewX(-0.25f);
//文本对齐方式
mPaint.setTextAlign(Align.LEFT);
mPaint.setTextAlign(Align.CENTER);
mPaint.setTextAlign(Align.RIGHT);
//计算制定长度的字符串(字符长度、字符个数、显示的时候真实的长度)
int breadText = mPaint.breakText(text, measureForwards, maxWidth, measuredWidth)
mPaint.setTextSize(50);
float[] measuredWidth = new float[1];
int breakText = mPaint.breakText(str, true, 200, measuredWidth);
Log.i("RICKY", "breakText="+breakText+", str.length()="+str.length()+", measredWidth:"+measuredWidth[0]);
// Rect bounds获取文本的矩形区域(宽高)
mPaint.getTextBounds(text, index, count, bounds)
mPaint.getTextBounds(text, start, end, bounds)
//获取文本的宽度,和上面类似,但是是一个比较粗略的结果
float measureText = mPaint.measureText(str);
//获取文本的宽度,和上面类似,但是是比较精准的。
float[] measuredWidth = new float[10];
//measuredWidth得到每一个字符的宽度;textWidths字符数
int textWidths = mPaint.getTextWidths(str, measuredWidth);
mPaint.getTextWidths(text, start, end, widths)
=====================基线的问题=====================
FontMetrics fontMetrics = mPaint.getFontMetrics();
fontMetrics.top;
fontMetrics.ascent;
fontMetrics.descent;
fontMetrics.bottom;
所有的四个值都是以基线baseLine为基准来计算的。baseline以上的就是负的;以下的是正的。
在做自定义控件的时候canvas.drawText(x,y),绘制文本的时候并不是text的左上角,而是以baseline为基准的。
1)实例:指定左上角的顶点坐标 绘制文本
公式: float baselineY = Y - fontMetrics.top; Y表示做左上角坐标
2)实例:指定中间位置,绘制文本 centerY表示中线坐标
公式: float baselineY = centerY + (fontMetrics.bottom-fontMetrics.top)/2 - fontMetrics.bottom
=getHeight()/2-(paint.descent()+paint.ascent())/2
2、Canvas:画布、画板。
canvas.drawLine(0, 0, 100, 100, paint);//绘制直线
float []pts = {0,0,100,100,200,200,300,300};
canvas.drawLines(pts, paint);
canvas.drawPoints(pts, paint);//点
RectF r = new RectF(100, 100, 400, 500);
canvas.drawRect(r, paint); //矩形
canvas.drawRect(left, top, right, bottom, paint);//矩形
canvas.drawRoundRect(r, 30, 30, paint);//圆角矩形
canvas.drawCircle(300, 300, 200, paint);//画圆
canvas.drawOval(r, paint);//画椭圆
canvas.drawArc(r, 0, 90, true, paint);//画圆弧
Path路径来绘制线条
Path path = new Path();
path.moveTo(100, 100);
path.lineTo(200, 100);
path.lineTo(200, 200);
path.cubicTo(250, 200, 350, 300, 450, 400);
path.close();
canvas.drawPath(path, paint);
//--------------------Canvas变换技巧--------------------------
//1.平移(Translate)
RectF r = new RectF(0, 0, 400, 500);
canvas.drawRect(r, paint);
paint.setColor(Color.BLUE);
//将画布平移
canvas.translate(50, 50);
//当canvas执行drawXXX的时候就会新建一个新的画布图层
canvas.drawRect(r, paint);
RectF r2 = new RectF(0, 0, 400, 500);
paint.setColor(Color.RED);
//虽然新建了一个画布图层,但是还是会沿用之前设置的平移变换。不可逆的。(save和restore来解决)
canvas.drawRect(r2, paint);
//2.缩放Scale
RectF r = new RectF(0, 0, 400, 500);
canvas.drawRect(r, paint);
paint.setColor(Color.BLUE);
//sx,sy:分别对x/y方向的一个缩放系数,画布的缩放会导致里面所有的绘制的东西都会有一个缩放效果
canvas.scale(1.5f, 0.5f);
canvas.drawRect(r, paint);
3.旋转Rotate
RectF r = new RectF(200, 200, 400, 500);
canvas.drawRect(r, paint);
paint.setColor(Color.BLUE);
canvas.rotate(45);
canvas.rotate(45, 200, 200);
canvas.drawRect(r, paint);
4.斜拉画布Skew
RectF r = new RectF(200, 200, 400, 500);
canvas.drawRect(r, paint);
paint.setColor(Color.BLUE);
//sx,sy倾斜度:X轴方向上倾斜60度,tan60=根号3
canvas.skew(1.73f, 0);
canvas.drawRect(r, paint);
//5.裁剪画布clip
RectF r = new RectF(200, 200, 400, 500);
canvas.drawRect(r, paint);
paint.setColor(Color.BLUE);
canvas.clipRect(new Rect(250, 250, 300, 400));
canvas.drawColor(Color.YELLOW);
Canvas画布的正确的理解:
1)当canvas执行drawXXX的时候就会新建一个新的画布图层
canvas.drawRect(r, paint);
2)虽然后面新建了一个画布图层,但是还是会沿用之前设置的平移变换。不可逆的。(save和restore来解决)
canvas.translate(50, 50);
//当canvas执行drawXXX的时候就会新建一个新的画布图层
canvas.drawRect(r, paint);
RectF r2 = new RectF(0, 0, 400, 500);
paint.setColor(Color.RED);
//虽然新建了一个画布图层,但是还是会沿用之前设置的平移变换。不可逆的。(save和restore来解决)
canvas.drawRect(r2, paint);
3、PathMeasure
这是一个用来测量Path的类,可以获取Path路径中相关的信息,比如任意长度一点的坐标,角度值、截取片段等。
利用这个类结合Path可以做出很多不错的动画效果。具体用法参考自行百度吧,这里就不一一列举了