经常使用自定义 View 方法汇总 Canvas 类(一)

canvas 类

1. 基本图形

1.1 颜色填充

drawColor(int color); 
drawRGB(int r, int g, int b);
drawARGB(int a, int r, int g, int b);
复制代码

1.2 画圆

// centerX centerY 圆心的坐标 
// radius 是圆的半径,单位都是像素
drawCircle(float centerX, float centerY, float radius, Paint paint); 
复制代码

1.3 画矩形

//left, top, right, bottom 矩形四条边的坐标
drawRect(float left, float top, float right, float bottom, Paint paint);
//RectF 或 Rect 绘制矩形的对象
drawRect(RectF rect, Paint paint);
drawRect(Rect rect, Paint paint);
复制代码

1.4 画点

// x 和 y 是点的坐标
//大小经过 paint.setStrokeWidth(width) 来设置
//形状经过 paint.setStrokeCap(cap) 来设置
//端点有圆头 (ROUND)、平头 (BUTT) 和方头 (SQUARE) 
drawPoint(float x, float y, Paint paint);
// 画点(批量)
// pts 点的坐标数组,每两个成一对;
// offset 表示跳过数组的前几个数再开始记坐标
// count 表示一共要绘制几个点
drawPoints(float[] pts, int offset, int count, Paint paint); drawPoints(float[] pts, Paint paint);
复制代码

1.5 画椭圆

// left, top, right, bottom 椭圆的左、上、右、下四个边界点的坐标
drawOval(float left, float top, float right, float bottom, Paint paint);
//RectF 来绘制椭圆
drawOval(RectF rect, Paint paint);
复制代码

1.6 画线

//startX, startY, stopX, stopY 线的起点和终点坐标。
drawLine(float startX, float startY, float stopX, float stopY, Paint paint);
//批量画线
drawLines(float[] pts, int offset, int count, Paint paint); drawLines(float[] pts, Paint paint);
复制代码

因为直线不是封闭图形,因此 setStyle(style) 对直线没有影响canvas

1.7 画圆角矩形

// left, top, right, bottom 是四条边的坐标,
// rx 和 ry 是圆角的横向半径和纵向半径
drawRoundRect(float left, float top, float right, float bottom, float rx, float ry, Paint paint)
drawRoundRect(RectF rect, float rx, float ry, Paint paint);
复制代码

1.8 画弧形或扇形

//left, top, right, bottom 描述的是这个弧形所在的椭圆
//startAngle 是弧形的起始角度(x 轴的正向,即正右的方向,是 0 度的位置;顺时针为正角度,逆时针为负角度),
//sweepAngle 是弧形划过的角度;
//useCenter 表示是否链接到圆心,若是不链接到圆心,就是弧形,若是链接到圆心,就是扇形
drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, Paint paint);
复制代码

1.9 画 Bitmap

drawBitmap(Bitmap bitmap, float left, float top, Paint paint);
drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint); drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint);  drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint);
复制代码

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);数组

2. 画自定义图形

//path 路径
drawPath(Path path, Paint paint);
复制代码

例:画心bash

// 初始化 Path 对象
Path path = new Path(); 
// 使用 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 描述的图形(心形),大功告成
复制代码

2.1 添加圆

// x, y, radius 圆的基本信息,dir 是画圆的路径的方向
addCircle(float x, float y, float radius, Direction dir);
复制代码

2.2 添加椭圆

addOval(float left, float top, float right, float bottom, Direction dir);
addOval(RectF oval, Direction dir);
复制代码

2.3 添加矩形

addRect(float left, float top, float right, float bottom, Direction dir); 
addRect(RectF rect, Direction dir);
复制代码

2.4 添加圆角矩形

addRoundRect(RectF rect, float rx, float ry, Direction dir);
addRoundRect(float left, float top, float right, float bottom, float rx, float ry, Direction dir);
addRoundRect(RectF rect, float[] radii, Direction dir);
addRoundRect(float left, float top, float right, float bottom, float[] radii, Direction dir);
复制代码

2.5 添加直线

// 绝对坐标
lineTo(float x, float y);
// 相对当前位置的相对坐标 relatively 初始值为原点 (0, 0)。
rLineTo(float x, float y);
复制代码

2.6 贝塞尔曲线

// 二次贝塞尔曲线
// 起点 当前位置,而参数中的 x1, y1 控制点 x2, y2终点的坐标
quadTo(float x1, float y1, float x2, float y2);
rQuadTo(float dx1, float dy1, float dx2, float dy2);
// 三次贝塞尔曲线
cubicTo(float x1, float y1, float x2, float y2, float x3, float y3);
rCubicTo(float x1, float y1, float x2, float y2, float x3, float y3);
复制代码

2.7 添加弧形

arcTo(RectF oval, float startAngle, float sweepAngle, boolean forceMoveTo);
arcTo(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean forceMoveTo);
arcTo(RectF oval, float startAngle, float sweepAngle);
//一个直接使用了 forceMoveTo = true 的简化版 arcTo() 
addArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle);
addArc(RectF oval, float startAngle, float sweepAngle);
复制代码

注:关于 forceMoveTo 参数:是否强制移动到开始位置。 true:强制移动,不会留下移动的痕迹 false:不强制,会留下移动的痕迹post

2.8 移动到目标位置

moveTo(float x, float y);
rMoveTo(float x, float y); 
复制代码

2.9 封闭当前子图形

//等价于方法 lineTo(起点坐标);
close();
复制代码

注:paint 的 style 设置为 FILL 或 FILL_AND_STROKE 时,自动封闭ui

2.10 设置填充方式

// EVEN_ODD
// WINDING (默认值)
// INVERSE_EVEN_ODD
// INVERSE_WINDING
setFillType(FillType fillType);
复制代码

效果展现 spa

参考:HenCoder Android 开发进阶: 自定义 View 1-1 绘制基础code

相关文章
相关标签/搜索