自定义View绘制的方式是重写onDraw()方法。 绘制的关键是画布(Cavans)和画笔(Paint)。画布提供基本信息,画笔提供所有基本信息之外的风格信息。
1. 画笔的常用方法:
mPaint.setStyle(Paint.Style.STROKE);//fill(default): 填充模式;stroke:勾边模式
paint.setStrokeWidth(20); // 线条宽度为 20 像素
mPaint.setColor(Color.RED);
mPaint.setAlpha(5); //透明度 1-255被缩放到0-1 mPaint.setTextSize(15f); //单位像素 mPaint.setAntiAlias(true); //是否抗锯齿
2. 画布:
绘制方法:以drawXXX()开头的一系列方法,包括画点、直线、矩形、圆、路径等;
//drawColor方法一般用来在绘制之前设置背景颜色或者绘制之后设置半透明蒙罩 canvas.drawColor(Color.BLACK); canvas.drawColor(Color.parseColor("#88880000"));//半透明红色
2.1 drawPath()方法
2.1.1 Path对象
Path对象有两种使用方式:1)直接描述路径和 2)辅助的设置或计算(极少用)。
直接描述路径:
I. 子图形的添加:各种.addXXX()方法
II. 划线: 画直线的.LineTo(x,y)、.rLineTo(dx,dy)方法和画弧形线的arcTo()方法。
III. 移动Path的当前像素点:.moveTo()方法。
IV. 将当前图形封闭: .close()。该方法等同于.lineTo(起点坐标)。