view启动流程
测量view的宽高 定位view的位置 绘制view的内容
onMeasure()---------------------->onLayout()---------------------onDraw()
依次如下
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
// getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
//传入这个方法的参数就是组件的最终宽高
setMeasuredDimension(300, 200);
}
//left、top、right、bottom是父空间传入的参数,这四个参数表明了组件的宽高和位置
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
// TODO Auto-generated method stub
super.onLayout(changed, left, top, right, bottom);
// System.out.println(left + " ; " + top + " ; " + right + " ; " + bottom);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
自定义view一般分为以下步骤
1、自定义类MyView继承自View
2、重写onMeasure方法,指定控件大小。
3、重写onDraw方法,绘制控件内容。
4、重写onTouchEvent方法,对touch事件进行解析。