這裏只是測試各個方法的執行流程,有些時候就是這些小基礎是非常重要的,下了測試代碼如下:
/**
* 自定義View的常用方法的調用先後順序
* Created by chengguo on 2016/3/18.
*/
public class CustomView extends View {
/**
* 用java代碼創建CustomView時調用此構成方法
*
* @param context
*/
public CustomView(Context context) {
super(context);
Log.i("tag", "---- public CoustomView(Context context) ----");
}
/**
* 用佈局文件xml創建CustomView時調用此構成方法
*
* @param context
*/
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i("tag", "---- public CoustomView(Context context, AttributeSet attrs) ----");
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.i("tag", "---- public CoustomView(Context context, AttributeSet attrs, int defStyleAttr) ----");
}
/**
* 使用佈局文件XML創建CustomView時,在xml文件加載完成後調用這個方法
*/
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Log.i("tag", "---- onFinishInflate() ----");
}
/**
* CustomView的大小發生改變時調用這個方法
*
* @param w
* @param h
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.i("tag", "---- onSizeChanged = " + " w = " + w + " h = " + h + " oldW = " + oldw + " oldH = " + oldw);
}
/**
* 在畫布上面繪製
*
* @param canvas
*/
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Log.i("tag", "---- dispatchDraw = ");
}
/**
* 繪製CustomView時調用
*
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i("tag", "---- onDraw ----");
}
/**
* 測量CustomView的大小
*
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i("tag", "---- onMeasure ----");
}
/**
* 將CustomView放置到父容器中去
*
* @param changed
* @param left
* @param top
* @param right
* @param bottom
*/
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.i("tag", "---- onLayout ----");
}
/**
* 將CustomView依附到Window中
*/
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.i("tag", "---- onAttachedToWindow ----");
}
/**
* 當手機屏幕從橫屏和豎屏 相互轉化時調用
*
* @param newConfig
*/
@Override
protected void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i("tag", "---- onConfigurationChanged ----");
}
}
下面是Log信息,如下:
03-18 11:32:30.198 14844-14844/? I/tag: ---- public CoustomView(Context context, AttributeSet attrs) ----
03-18 11:32:30.198 14844-14844/? I/tag: ---- onFinishInflate() ----
03-18 11:32:30.227 14844-14844/? I/tag: ---- onAttachedToWindow ----
03-18 11:32:30.228 14844-14844/? I/tag: ---- onMeasure ----
03-18 11:32:30.294 14844-14844/? I/tag: ---- onMeasure ----
03-18 11:32:30.295 14844-14844/? I/tag: ---- onSizeChanged = w = 1080 h = 135 oldW = 0 oldH = 0
03-18 11:32:30.295 14844-14844/? I/tag: ---- onLayout ----
03-18 11:32:30.310 14844-14844/? I/tag: ---- onMeasure ----
03-18 11:32:30.310 14844-14844/? I/tag: ---- onLayout ----
03-18 11:32:30.311 14844-14844/? I/tag: ---- onDraw ----
03-18 11:32:30.311 14844-14844/? I/tag: ---- dispatchDraw =
經過log測試,就知道了,View的方法的執行過程,那麼在自定義View的時候,就會在正確的方法中,進行操作。希望對大家有所幫助,原文鏈接