一切的开始 onDraw()
/**
* 1、圆
*/
/**
* FILL , STROKE 和 FILL_AND_STROKE 。
* FILL 是填充模式,
* STROKE 是画线模式(即勾边模式)
* FILL_AND_STROKE 是两种模式一并使用:既画线又填充。它的默认值是 FILL ,填充模式。
*/
mPaint.setStyle(Paint.Style.FILL);//设置绘制模式
// mPaint.setStyle(Paint.Style.STROKE);//环
mPaint.setStrokeWidth(20);//线条宽度
mPaint.setColor(Color.RED); // 设置为红色
mPaint.setAntiAlias(true);//开启抗锯齿 抗锯齿并不一定适合所有场景。
//绘制一个圆
canvas.drawColor(Color.BLUE);//绘制背景
canvas.drawCircle(300, 300, 200, mPaint);
canvas.drawColor(Color.parseColor("#88880000"));
/**
* 2、矩形
*/
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLACK);
mPaint.setAntiAlias(true);
//绘制矩形
canvas.drawRect(100, 100, 400, 400, mPaint);
Paint mPaint2 = new Paint();
mPaint2.setStyle(Paint.Style.STROKE);
mPaint2.setStrokeWidth(10);
mPaint2.setColor(Color.BLACK);
canvas.drawRect(500, 100, 800, 395, mPaint2);
/**
* 画点
*/
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(20);
//默认为方形
// 端点有圆头( ROUND )、平头 ( BUTT ) 和方头 ( SQUARE ) 三种 SQUARE 或 BUTT 画出来是方形的点。
mPaint.setStrokeCap(Paint.Cap.BUTT);
canvas.drawPoint(50, 50, mPaint);
/**
* 画点 批量
*/
float points[] = {0, 0, 50, 50, 100, 100, 50, 100, 100, 50};
mPaint.setColor(Color.BLACK);
mPaint.setStrokeWidth(20);
mPaint.setStrokeCap(Paint.Cap.ROUND);
// canvas.drawPoints(points, mPaint);
canvas.drawPoints(points, 2/**跳过两个数,即前两个 0*/, 8/** 一共绘制 8 个数(4 个点)*/, mPaint);
/**
* 画椭圆
*/
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawOval(400, 50, 700, 200, mPaint);
}
RectF rectF = new RectF();
rectF.left = 100;
rectF.top = 150;
rectF.right = 300;
rectF.bottom = 400;
canvas.drawOval(rectF, mPaint);
//椭圆环
RectF rectF2 = new RectF();
rectF2.left = 400;
rectF2.top = 50;
rectF2.right = 700;
rectF2.bottom = 200;
Paint mPaint2 = new Paint();
mPaint2.setColor(Color.BLACK);
mPaint2.setStyle(Paint.Style.STROKE);
mPaint2.setStrokeWidth(2);
canvas.drawOval(rectF2, mPaint2);
/**
* 画线条
*/
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(10);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLACK);
canvas.drawLine(50, 50, 500, 90, mPaint);
//线条 批量
float point[] = {20, 20, 120, 20, 70, 20, 70, 120, 20, 120, 120, 120, 200, 20, 300, 20, 300, 20, 300, 120, 200, 120, 300, 120, 200, 20, 200, 120};
canvas.drawLines(point, mPaint);
/**
* 画圆角矩形
*/
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLACK);
mPaint.setAntiAlias(true);
RectF rect = new RectF();
rect.left = 200;
rect.top = 200;
rect.right = 500;
rect.bottom = 400;
canvas.drawRoundRect(rect, 50, 50, mPaint);
/**
* 画弧形
*/
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.BLACK);
//(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean useCenter, @NonNull Paint paint)
// left(x) , top(y) , right(x) , bottom(y) 描述的是这个弧形所在的椭圆
RectF rect = new RectF();
rect.left = 200;
rect.top = 100;
rect.right = 800;
rect.bottom = 500;
// startAngle 是弧形的起始角度(x 轴的正向,即正右的方 向,是 0 度的位置;顺时针为正角度,逆时针为负角度)
// sweepAngle 是弧形划 过的角度
// useCenter 表示是否连接到圆心,如果不连接到圆心,就是弧形,如果 连接到圆心,就是扇形。
canvas.drawArc(rect, -100, 100, true, mPaint);//画扇形
canvas.drawArc(rect, 20, 140, false, mPaint);//画弧形
//画弧线
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawArc(rect, 180, 60, false, mPaint);//画弧线
/**
* ********* Path 方法第一类:直接描述路径。
* addXxx() ——添加子图形
* xxxTo() ——画线(直线或曲线)
* rxxxTO()——由当前位置 接着画
*/
/**
* 画心形
*/
mPaint.setColor(Color.BLACK);
mPaint.setAntiAlias(true);
Path mPath = new Path();
RectF rect = new RectF();
rect.left = 200;
rect.top = 200;
rect.right = 400;
rect.bottom = 400;
mPath.addArc(rect, -225, 225);
RectF rect2 = new RectF();
rect2.left = 400;
rect2.top = 200;
rect2.right = 600;
rect2.bottom = 400;
// arcTo() 只用来画弧形而不画扇形
//这个 forceMoveTo 参数的意思是,绘制是要「抬一下笔移动过去」,还是「直接拖着笔过去」,区别在于是否留下移动的痕迹。
//forceMoveTo true 强制移动到弧形起点 false拖着笔画过去
mPath.arcTo(rect2, -180, 225, false);
mPath.lineTo(400, 542);//从当前位置向目标位置画一条直线 rLineTo(x, y) 的参数是相对当前位置的相对坐标
canvas.drawPath(mPath, mPaint);
/**
* 画三角形
* close() 封闭当前子图形
*/
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStyle(Paint.Style.FILL);
Path mPath = new Path();
mPath.moveTo(100, 100);//移动到目标位置
mPath.lineTo(200, 100);
mPath.lineTo(150, 200);
mPath.close();//使用 close() 封闭子图形。等价于 path.lineTo(100, 100)
canvas.drawPath(mPath, mPaint);
/**
*************** Path 方法第二类:辅助的设置或计算
* (这类方法的使用场景比较少)
* Path.setFillType(Path.FillType ft) 设置填充方式
* EVEN_ODD
* WINDING (默认值)
* INVERSE_EVEN_ODD
* INVERSE_WINDING
* 简单粗暴版的总结: WINDING 是「全填充」,而 EVEN_ODD 是「交叉填充」
*/
/**
* 绘制Bitmap
* 绘制 Bitmap 对象,也就是把这个 Bitmap 中的像素内容贴过来。
* 其中 left 和 top 是要把 bitmap 绘制到的位置坐标。
*/
// canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)
// canvas.drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
// canvas.drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
//还有一个兄弟方法 drawBitmapMesh() ,可以绘制具有网格拉伸效果的 Bitmap。
/***
* 绘制文字
*/
mPaint.setTextSize(60);
canvas.drawText("Hello World!", 200, 100, mPaint);