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事件進行解析唾糯。