笔记如下
如图:
1.画线
public void line(View view){
//1.划线
//8888:高清图
//先准备画布
Bitmap bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
//2.准备画板
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
//3.划线
canvas.drawLine(100,100,500,500,paint);
iv.setImageBitmap(bitmap);
}
2.画矩形
drawRect(float left, float top, float right, float bottom, @NonNull Paint paint)
矩形的实现方法left和right参照同一边,top和bottom也是一样
public void rect(View view){
//1.划矩形
//8888:高清图
Bitmap bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
//2.准备画布
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
//设置颜色
paint.setColor(Color.RED);
//设置画笔的样式
paint.setStyle(Paint.Style.STROKE);//中空
paint.setStrokeWidth(5);//设置粗细
//3.划线
canvas.drawRect(30,30,500,500,paint);
iv.setImageBitmap(bitmap);
}
3.画圆
public void circle(View view){
//1.划圆
//8888:高清图
Bitmap bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
//2.准备画布
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
//设置颜色
paint.setColor(Color.RED);
//设置画笔的样式
//paint.setStyle(Paint.Style.STROKE);//中空
//paint.setStrokeWidth(10);//设置粗细
//3.划线
canvas.drawCircle(250,250,200,paint);
//cx,cy:指定圆心
//radius:指定半径
iv.setImageBitmap(bitmap);
}
4.画扇形
public void arc(View view){
//1.划扇形
//8888:高清图
Bitmap bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
//2.准备画布
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
//设置颜色
paint.setColor(Color.RED);
//设置画笔的样式
//paint.setStyle(Paint.Style.STROKE);//中空
//paint.setStrokeWidth(10);//设置粗细
//3.划扇形
RectF rect = new RectF(20,20,500,500);
float startAngle = 0;//起始角度
float sweepAngle = 120;//以起始角度为开始,扫过120度
boolean userCenter = true;//是否画中心
canvas.drawArc(rect,startAngle,sweepAngle,userCenter,paint);
iv.setImageBitmap(bitmap);
}
5.雨滴
就是一个三角形和一个扇形
如何画三角形呢?
可以指定画的路径,连接指定的点
Path path = new Path();
path.moveTo(x1,y1);//将画笔移动到点1
path.lineTo(x2,y2);//连线点2
public void trangle(View view){
//1.划三角形
//8888:高清图
Bitmap bitmap = Bitmap.createBitmap(500,500, Bitmap.Config.ARGB_8888);
//2.准备画布
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
//设置颜色
paint.setColor(Color.RED);
//设置画笔的样式
//paint.setStyle(Paint.Style.STROKE);//中空
//paint.setStrokeWidth(10);//设置粗细
//3.划三角形
//RectF rect = new RectF(20,20,500,500);
float x1 = 250;
float y1 = 0;
float x2 = 200;
float y2 = 300;
float x3 = 300;
float y3 = 300;
Path path = new Path();
path.moveTo(x1,y1);//将画笔移动到点1
path.lineTo(x2,y2);//连线点2
path.arcTo(new RectF(200,250,300,350),0,180);
path.lineTo(x3,y3);//连线点3
path.lineTo(x1,y1);//连线点4
path.close();
canvas.drawPath(path,paint);
iv.setImageBitmap(bitmap);
}