view的执行过程,各种方法的调用先后顺序

来源:互联网 发布:2015中国国际储备数据 编辑:程序博客网 时间:2024/06/07 15:00

这里只是测试各个方法的执行流程,有些时候就是这些小基础是非常重要的,下了测试代码如下:

/**  * 自定义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的时候,就会在正确的方法中,进行操作。希望对大家有所帮助,原文链接