Introduction
Android中View的绘制有三个步骤,measure->layout->draw.
- measure:测量view的宽高(并不一定是在视图上的最终宽高)
- layout:得到view在父布局上的位置,并确定view的最终宽高
- draw:将view绘制到界面上
具体步骤
新建一个类然后继承自view,并创建构造方法
public class SquareProgress extends View{
public SquareProgress(Context context) {
super(context);
}
}
添加我们需要用到的属性
这里根据我们不同的需求可以添加不同的属性,这里只求最基本的功能,所以属性不是很多。
用到的一些基本属性。这里定义了三只画笔Paint,画笔的属性有:画笔的颜色、宽度、模式Stytle(用系统常量);进度条的总长度,可以自己定义(常规的是按照百分比来的);当前进度;整个view的宽高;进度条所在矩形的宽高 ;当前进度前端小圆点的坐标。(有些属性是为了我方便后面引用而不是view的固有属性)
private String TAG="SquareProgress";
//各个画笔的颜色
private int maxColor= Color.GRAY;//总进度条颜色为灰色
private int curColor=Color.BLUE;//当前进度条颜色为蓝色
private int dotColor=Color.RED;//进度条前端的小圆点为红色
private float allLength;//进度条的总长度
private int maxProgress=100;//总的进度条长度为100(可改变)
private int curProgress=30;//当前进度为30(可改变)
private Paint curPaint;//当前进度条的画笔
private Paint maxPaint;//总进度条的画笔
private Paint dotPaint;//进度条前端小圆点的画笔
private int width;//整个view的宽度,(包括paddingleft和paddingright)
private int height;//整个view的高度,(包括paddingtop和paddingbottom)
private float maxProgressWidth;//整个进度条画笔的宽度
private float curProgressWidth;//当前进度条画笔的宽度
private float dotDiameter;//进度条顶端小圆点的直径
private RectShape maxRectShape;
private RectShape curRectShape;
private boolean canDisplayDot=true;//是否显示小圆点
private Path curPath;//当前进度条的路径,(总的进度条的路径作为onDraw的局部变量)
private float proWidth;//整个进度条构成矩形的宽度
private float proHeight;//整个进度条构成矩形的高度
private float dotCX;//小圆点的X