很多时候android本身提供的控件并不能满足我们实现一些复杂或是酷炫的效果,这时候就得自定义View了,当然这里不会写写关于自定义View的,只是介绍下canvas常见的方法。

画圆弧:drawArc

drawArc(@NonNull RectF oval, float startAngle, float                                                    sweepAngle, boolean useCenter, @NonNull Paint paint)

drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)

具体从参数方面来说可以绘制圆、椭圆、圆环、圆弧、扇形。
RectF oval:表示一个矩形,即绘制范围,正方形范围绘制圆,长方形范围绘制椭圆
float left, float top, float right, float bottom:可组成矩形
float startAngle:起始绘制角度,中心为原点,水平向右为0度,按顺时针方向绘制
float sweepAngle:圆弧扫过的角度,值360则绘制圆、椭圆、圆环,否则绘制圆弧、扇形,亦相当于设置进度条进度(0-360),是圆形进度条的实现方法之一
boolean useCenter:true绘制扇形,false绘制圆弧
Paint paint:设置画笔参数,以下将列出常用方法:

paint.setAntiAlias(true);// 抗锯齿
paint.setStyle(Paint.Style.STROKE);// 线条画笔,可绘制圆环
paint.setColor(Color.RED);// 设置画笔颜色
paint.setStrokeWidth(10);// 设置画笔粗细
paint.setShader(shader);// 设置颜色渐变,比如在这里可绘制彩色圆环进度条
paint.setStrokeCap(Paint.Cap.ROUND);// 设置圆弧两端为圆弧闭合

渐变Shader:
LinearGradient:线性渐变:
LinearGradient(float x0, float y0, float x1, float y1, int color0, int color1, TileMode tile)

画位图:drawBitmap

drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect(F) dst, @Nullable Paint paint)

Bitmap bitmap:要绘制的bitmap
Rect src:要绘制的bitmap的某部分或全部
RectF dst:绘制bitmap的位置
Paint paint:画笔,目测不知道有啥用
drawBitmap(@NonNull Bitmap bitmap, float left, float top, @Nullable Paint paint)

Bitmap bitmap:要绘制的bitmap
float left:左边偏移量
float top:顶部偏移量
Paint paint:画笔

填充颜色:drawColor

drawColor(@ColorInt int color, @NonNull PorterDuff.Mode mode)

int color:绘制的画布颜色
Mode mode:绘制模式

画线:drawLine

drawLine(float startX, float startY, float stopX, float stopY,
         @NonNull Paint paint)

float startX:线条起始x坐标,左上角坐标(0, 0)
float startY:线条起始y坐标
float stopX:线条终止x坐标
float stopY:线条终止y坐标
Paint paint:画笔

画笔绘制:drawPaint

drawPaint(@NonNull Paint paint)

Paint.Style.STROKE:给画布画边框
Paint.Style.FILL:给画布填充颜色

画矩形:drawRect(F)

drawRect(F)(@NonNull RectF rect, @NonNull Paint paint)

Paint.Style.STROKE:画边框,大小为rect
Paint.Style.FILL:画背景,大小为rect

画圆:drawCircle

drawCircle(float cx, float cy, float radius, @NonNull Paint paint)

float cx, float cy:圆心坐标(cx, cy)
Paint paint:画笔,STROKE空心;FILL实心

画椭圆:drawOval

drawOval(@NonNull RectF oval, @NonNull Paint paint)
drawOval(float left, float top, float right, float bottom, @NonNull Paint paint)

RectF oval = new RectF(left, top, right, bottom);
RectF oval:绘制区域
Paint paint:画笔,STROKE空心;FILL实心

画点:drawPoint

drawPoint(float x, float y, @NonNull Paint paint)

float x, float y:画点位置(x, y)
Paint paint:画笔

画圆角矩形:drawRoundRect

drawRoundRect(@NonNull RectF rect, float rx, float ry, @NonNull Paint paint)
drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, @NonNull Paint paint)

RectF rect = new RectF(left, top, right, bottom);
RectF rect:绘制区域
float rx, float ry:圆角半径
Paint paint:画笔

drawText

drawText(@NonNull char[] text, int index, int count, float x, float y, @NonNull Paint paint)

drawText(@NonNull String text, float x, float y, @NonNull Paint paint)

drawText(@NonNull String text, int start, int end, float x, float y, @NonNull Paint paint)

drawText(@NonNull CharSequence text, int start, int end, float x, float y, @NonNull Paint paint)

int start, int end:截取部分文本绘制
float x, float y:再坐标(x, y)处开始绘制文本
Paint paint:可设置字体大小等参数

绘制路径:drawPath

这个方法可以绘制比较复杂的一些函数曲线,这个不太熟悉,各位看官还是谷歌百度吧,这里简单介绍下常见方法:

drawPath(@NonNull Path path, @NonNull Paint paint)

Path path:绘制路径
Paint paint:画笔

画个三角形:
path.moveTo(0, getHeight()/4);
path.lineTo(0, (getHeight() * 3) / 4);
path.lineTo(getWidth(), getHeight()*3/4);
path.close();

画圆弧:相当于drawArc,参数useCenter为false
addArc(RectF oval, float startAngle, float sweepAngle)
addArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle)

画圆弧:默认闭合,forceMoveTo为true即圆弧,不闭合
arcTo(RectF oval, float startAngle, float sweepAngle)
arcTo(RectF oval, float startAngle, float sweepAngle, boolean forceMoveTo)

画圆:圆心(x, y),半径radius,dir绘制方向
addCircle(float x, float y, float radius, Direction dir)

画椭圆:
addOval(RectF oval, Direction dir)
addOval(float left, float top, float right, float bottom, Direction dir)

画矩形:
addRect(RectF rect, Direction dir)
addRect(float left, float top, float right, float bottom, Direction dir)

画圆角矩形:
addRoundRect(RectF rect, float rx, float ry, Direction dir)
addRoundRect(float left, float top, float right, float bottom, float rx, float ry, Direction dir)

画曲线:
cubicTo(float x1, float y1, float x2, float y2, float x3, float y3)
在(x1, y1)和(x3, y3)之间画曲线,以(x2, y2)为转折点

画(曲)线:
quadTo(float x1, float y1, float x2, float y2)
(x1, y1)是控制点,(x2, y2)是终点,起点是当前点,默认(0, 0),可用moveTo设置当前点。