# Canvas

//绘制一个圆
canvas.drawCircle(300, 300, 200, paint);

centerX centerY 是圆心的坐标，第三个参数 radius 是圆的半径，单位都是像素。



//颜色填充
canvas.drawColor(Color.BLACK);

canvas.drawColor(Color.parse("#88880000"); //半透明红色
canvas.drawRGB(100, 200, 100);
canvas.drawARGB(100, 100, 200, 100);


//绘制一个矩形
canvas.drawRect(100, 100, 500, 500, paint);

float left, float top, float right, float bottom, Paint paint

//画点
canvas.drawPoint(50, 50, paint);

paint.setStrokeWidth(20);
paint.setStrokeCap(Paint.Cap.ROUND);
canvas.drawPoint(50, 50, paint);

//批量画点，绘制四个点：(50, 50) (50, 100) (100, 50) (100, 100)
float[] points = {0f, 0, 50, 50, 50, 100, 100, 50, 100, 100, 150, 50, 150, 100};
canvas.drawPoints(points, 2/*跳过两个数，既前两个0*/, 8/*一共绘制8个数(4个点)*/, paint);

//画椭圆
canvas.drawOval(50, 50, 350, 200, paint);

示例图：

//画线，所得的结果是一条斜着的线
canvas.drawLine(200, 200, 800, 500, paint);

paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(10);
canvas.drawLine(200,200,800,500,paint);

//批量画线
float[] points = {20, 20, 120, 20, 70, 20, 70, 120, 20, 120, 120, 120, 150, 20, 250, 20, 150, 20, 150, 120, 250, 20, 250, 120, 150, 120, 250, 120};
canvas.drawLines(points, paint);

//画圆角矩形
canvas.drawRoundRect(100,100,500,300,50,50,paint);

left, top, right, bottom 是四条边的坐标，rx 和 ry 是圆角的横向半径和纵向半径。

paint.setStrokeWidth(10);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRoundRect(100,100,500,300,50,50,paint);

示例图：

//绘制弧形或扇形
canvas.drawArc(200,100,800,500,-100,100,false,paint);

left, top, right, bottom 描述的是这个弧形所在的椭圆；startAngle 是弧形的起始角度（x 轴的正向，即正右的方向，是 0 度的位置；顺时针为正角度，逆时针为负角度），sweepAngle 是弧形划过的角度；useCenter 表示是否连接到圆心，如果不连接到圆心，就是弧形，如果连接到圆心，就是扇形。

paint.setStyle(Paint.Style.FILL);
//绘制扇形
canvas.drawArc(200,100,800,500,-100,100,false,paint);
//绘制弧形
canvas.drawArc(200,100,800,500,20,140,false,paint);
paint.setStyle(Paint.Style.STROKE);
//绘制不封口的弧形
canvas.drawArc(200,100,800,500,180,60,false,paint);

Paint.Style.FILL是封口，Paint.Style.STROKE是不封口。


//绘制自定义形状
canvas.drawPath(path, paint);

Path path = new Path();
path.addArc(200, 200, 400, 400, -225, 225);
path.arcTo(400, 200, 600, 400, -180, 225, false);
path.lineTo(400, 542);
canvas.drawPath(path, paint);

Path可以理解为是一个封装好的绘制模块，Path里面可以绘制所有canvas直接绘制的图形，只不过绘制好后保存在path变量里，之后调用canvas.drawPath(path, paint)来进行绘制。

11-29 614
04-17 3222

01-21 1万+
11-29 663
11-28 8万+
09-05 6万+
01-21 8万+
06-06 2万+
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客