view的生命周期方法:
/** *当View中所有的子控件均被映射成xml后触发 */ @Override protected void onFinishInflate() { super.onFinishInflate(); Log.e("flag","onFinishInflate"); } /** * 当view被附着到一个窗口时触发 */ @Override protected void onAttachedToWindow() { Log.e("flag","onAttachedToWindow"); super.onAttachedToWindow(); } /** * 当窗口中包含的可见的view发生变化时触发 * @param visibility */ @Override protected void onWindowVisibilityChanged(int visibility) { Log.e("flag","onWindowVisibilityChanged"); super.onWindowVisibilityChanged(visibility); } /** * 测量控件的宽高 生命周期 * @param widthMeasureSpec * @param heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); Log.e("flag","onMeasure"); /** * 如果我们想要的type是圆形,则我们需要让宽高一致,以较小的值为准、 */ if(type == TYPE_CIRCLE){ mWinth = Math.min(getMeasuredWidth(),getMeasuredHeight()); mRadius = mWinth /2; setMeasuredDimension(mWinth, mWinth); } } /** * 当大小改变的时候调用这个方法,最少调用一次 * @param w * @param h * @param oldw * @param oldh */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { Log.e("flag","onSizeChanged"); super.onSizeChanged(w, h, oldw, oldh); if(type == TYPE_ROUND){ rectF = new RectF(0,0,w,h); } } /** * 当view分配所有子元素的大小和位置时触发 * @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.e("flag","onLayout"); } @Override protected void onDraw(Canvas canvas) { // super.onDraw(canvas); 一定要注意,把这个方法去点 // Log.e("flag","onSizeChanged"); Log.e("flag", "onDraw"); if (getDrawable() == null) { return; } // canvas.drawRect(); setUpShader(); if (type == TYPE_ROUND) { canvas.drawRoundRect(rectF, mBorderRadius, mBorderRadius, mBitmapPaint); } else { canvas.drawCircle(mRadius, mRadius, mRadius, mBitmapPaint); // drawSomeThing(canvas); } } /** * 当view离开附着的窗口时触发 */ @Override protected void onDetachedFromWindow() { Log.e("flag","onDetachedFromWindow"); super.onDetachedFromWindow(); }
log日志如下:
06-18 22:08:27.613 18990-18990/com.qkhl.mycustomerview E/flag: onFinishInflate
06-18 22:08:27.629 18990-18990/com.qkhl.mycustomerview E/flag: onAttachedToWindow
06-18 22:08:27.629 18990-18990/com.qkhl.mycustomerview E/flag: onWindowVisibilityChanged
06-18 22:08:27.630 18990-18990/com.qkhl.mycustomerview E/flag: onMeasure
06-18 22:08:27.641 18990-18990/com.qkhl.mycustomerview E/flag: onMeasure
06-18 22:08:27.676 18990-18990/com.qkhl.mycustomerview E/flag: onSizeChanged
06-18 22:08:27.676 18990-18990/com.qkhl.mycustomerview E/flag: onLayout
06-18 22:08:27.679 18990-18990/com.qkhl.mycustomerview E/flag: onWindowFocusChanged
06-18 22:08:27.682 18990-18990/com.qkhl.mycustomerview E/flag: onMeasure
06-18 22:08:27.683 18990-18990/com.qkhl.mycustomerview E/flag: onMeasure
06-18 22:08:27.683 18990-18990/com.qkhl.mycustomerview E/flag: onLayout
06-18 22:08:27.683 18990-18990/com.qkhl.mycustomerview E/flag: onDraw
06-18 22:08:31.787 18990-18990/com.qkhl.mycustomerview E/flag: onWindowFocusChanged
06-18 22:08:32.044 18990-18990/com.qkhl.mycustomerview E/flag: onWindowVisibilityChanged
06-18 22:08:32.205 18990-18990/com.qkhl.mycustomerview E/flag: onDetachedFromWindow