1.系统提供了Canvas对象来提供绘图方法,里面有各种绘制图像的API,如drawPoint(点)、drawLine(线)、drawRect(矩形)、drawArc(弧)、drawCircle(圆)。Paint作为画笔,提供了多样的方法设置画笔的属性。
Paint paint = new Paint();
paint.setAntiAlias(true); //是否设置画笔的锯齿效果
paint.setColor(Color.GREEN); // 设置画笔的颜色
paint.setARGB(Color.BLACK, Color.RED, Color.GREEN, Color.BLUE); //设置画笔的ARGB值
paint.setAlpha(Color.CYAN); // 设置画笔的alpha值
paint.setTextSize(12); // 设置字体的大小
paint.setStyle(Paint.Style.FILL);// 设置画笔的风格(空心或者是实心)
paint.setStrokeWidth(2);// 设置空心边框的宽度
paint.style.fill与paint.style.stroke的区别:
绘制点:
canvas.draw(x,y,paint) // 圆心坐标,以及paint
绘制线:
canvas.drawLine(startX,startY,endX,endY,paint); // 起点坐标,终点坐标,paint
绘制连续的线段:
float[]pts={startX1,startY1,endX1,endY1,
startX2,startY2,endX2,endY2,
....
startXn,startYn,endXn,endYn,}
float[] pts={1,2,3,4,
4,5,6,7,
8,9,10,11};
canvas.drawLines(pts,paint);
绘制矩形:
canvas.drawRect(left,top,right,bottom,paint);//距离父布局的左上右下的距离,paint
绘制圆角矩形:
canvas.drawRoundRect(left,top,right,bottom,radiusX,radiusY,paint)
// 距离父布局的左上右下的距离,椭圆的坐标,paint
绘制圆:
canvas.drawCircle(circleX,circleY,radius,paint)//圆心坐标,半径,paint
绘制弧形:
canvas.drawArc(left, top, right, bottom, startAngle,
sweepAngle, useCenter, paint)//圆弧的左上右下,圆弧的起始角度,圆弧的角度,paint。其中useCenter的意思:useCenter If true, include the center of the oval in the arc, and close it if it is being stroked. This will draw a wedge(是否将圆弧进行封闭)
Paint.Style.STROKE+useCenter(true);//空心封闭的圆弧
Paint.Style.STROKE+useCenter(false);//空心不封闭的圆弧
Paint.Style.FILL+useCenter(true);//实心封闭的圆弧
Paint.Style.FILL+useCenter(false);//实心不封闭的圆弧
绘制文本:
canvas.drawText(text,startX,startY,paint)// 文本,文字的起点坐标,paint
在指定的位置绘制文本
canvas.drawPosText(text,new float[] {x1,y1,x2,y2…xn,yn},paint)//文本,文字的坐标,paint
绘制路径:
Path path = new Path();
path.moveTo(20,20);//坐标
path.lineTo(200, 200);//坐标
path.lineTo(300, 20);//坐标
canvas.drawPath(path, paint);
canvas.drawPath(path,paint)