大家都知道Activity的生命周期,那view的生命周期呢?除了常见的onMeasure,onLayout,onDraw,这些和Activity中的onCreate、onStart、onResume的执行先后顺序是什么呢?
View的生命周期从构造方法开始依次执行以下几个方法,该View的可见性为默认值时:
(0)Constructors()
View在代码中被创建时调用第一种构造方法,View从layout中加载出来时会被调用第二种构造方法,其中XML中的属性也会被解析。
(1)onFinishInflate()
该方法当View及其子View从XML文件中加载完成后触发调用。通常是在Activity中的onCreate方法调用后调用。
(2)onAttachedToWindow()
当View被附着到一个窗口时触发。在Activity第一次执行完onResume方法后被调用。
(3)onWindowVisibilityChanged()
包含当前View的Window可见性改变时被调用。
(4)onVisibilityChanged()
该方法在当前View或其祖先的可见性改变时被调用。如果View状态不可见或者GONE,该方法会第一个被调用。
(5)onMeasure()
该方法确定View以及其子View尺寸大小时被调用。
(6)onSizeChanged()
该方法在Measure方法之后且测量大小与之前不一样的时候被调用。
(7)onLayout()
该方法在当前View需要为其子View分配尺寸和位置时会被调用。
(8)onDraw(Canvas)
该方法用于View渲染内容的细节。
(9)onWindowFocusChanged()
该方法也可能在绘制过程中被调用,具体是在包含当前View的Window获得或失去焦点时被调用。此时可以设置代码中定义的View的一些LayoutParameter。
如果View进入了销毁阶段,肯定是会被调用的。
(10)onDetachedFromWindow()
当View离开附着的窗口时触发,比如在Activity调用onDestroy方法时View就会离开窗口。和一开始的AttachedToWindow相对,都只会被调用一次。
代码如下:
package com.example.testviewlifecycle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class CustomView extends View {
private static final String TAG = "CustomView=-->";
private Paint mPaint;
public CustomView(Context context) {
super(context);
Log.d(TAG, "view Constructors 1");
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
Log.d(TAG, "view Constructors 2");
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
Log.d(TAG, "view Constructors 3");
}
private void init() {
mPaint = new Paint();
mPaint.setColor(Color.RED);
mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setAntiAlias(true);
mPaint.setTextSize(36);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Log.d(TAG, "onFinishInflate");
}
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
Log.d(TAG, "onVisibilityChanged");
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Log.d(TAG, "onFocusChanged");
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.d(TAG, "onAttachedToWindow");
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Log.d(TAG, "onDetachedFromWindow");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.d(TAG, "onMeasure");
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.d(TAG, "onLayout");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d(TAG, "onDraw");
canvas.drawText("view生命周期", 100, 400, mPaint);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.d(TAG, "onSizeChanged");
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
Log.d(TAG, "onWindowFocusChanged");
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
Log.d(TAG, "onWindowVisibilityChanged");
}
}
这里默认的customView是visible的,和Activity的生命周期结果如下
1)、android:visibility=“visible”
创建过程
2020-03-10 14:38:06.542 7961-7961/com.example.testviewlifecycle D/MainActivity=-->: onCreate 1
2020-03-10 14:38:06.564 7961-7961/com.example.testviewlifecycle D/CustomView=-->: view Constructors 2
2020-03-10 14:38:06.564 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onFinishInflate
2020-03-10 14:38:06.566 7961-7961/com.example.testviewlifecycle D/MainActivity=-->: onCreate 2
2020-03-10 14:38:06.569 7961-7961/com.example.testviewlifecycle D/MainActivity=-->: onStart
2020-03-10 14:38:06.572 7961-7961/com.example.testviewlifecycle D/MainActivity=-->: onResume
2020-03-10 14:38:06.589 7961-7961/com.example.testviewlifecycle D/MainActivity=-->: onAttachedToWindow
2020-03-10 14:38:06.590 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onAttachedToWindow
2020-03-10 14:38:06.590 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onWindowVisibilityChanged
2020-03-10 14:38:06.590 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onVisibilityChanged
2020-03-10 14:38:06.593 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onMeasure
2020-03-10 14:38:06.594 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onMeasure
2020-03-10 14:38:06.606 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onMeasure
2020-03-10 14:38:06.606 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onMeasure
2020-03-10 14:38:06.606 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onSizeChanged
2020-03-10 14:38:06.606 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onLayout
2020-03-10 14:38:06.608 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onDraw
2020-03-10 14:38:06.621 7961-7961/com.example.testviewlifecycle D/CustomView=-->: onWindowFocusChanged
销毁过程
2020-03-10 14:41:24.684 9188-9188/com.example.testviewlifecycle D/MainActivity=-->: onPause
2020-03-10 14:41:24.694 9188-9188/com.example.testviewlifecycle D/CustomView=-->: onWindowFocusChanged
2020-03-10 14:41:25.237 9188-9188/com.example.testviewlifecycle D/CustomView=-->: onWindowVisibilityChanged
2020-03-10 14:41:25.252 9188-9188/com.example.testviewlifecycle D/MainActivity=-->: onStop
2020-03-10 14:41:25.255 9188-9188/com.example.testviewlifecycle D/CustomView=-->: onVisibilityChanged
2020-03-10 14:41:25.258 9188-9188/com.example.testviewlifecycle D/MainActivity=-->: onDestroy
2020-03-10 14:41:25.259 9188-9188/com.example.testviewlifecycle D/CustomView=-->: onDetachedFromWindow
2020-03-10 14:41:25.260 9188-9188/com.example.testviewlifecycle D/MainActivity=-->: onDetachedFromWindow
2)、**android:visibility=“invisible”**不可见时
创建
2020-03-10 14:42:54.333 9649-9649/? D/MainActivity=-->: onCreate 1
2020-03-10 14:42:54.404 9649-9649/? D/CustomView=-->: view Constructors 2
2020-03-10 14:42:54.404 9649-9649/? D/CustomView=-->: onFinishInflate
2020-03-10 14:42:54.413 9649-9649/? D/MainActivity=-->: onCreate 2
2020-03-10 14:42:54.417 9649-9649/? D/MainActivity=-->: onStart
2020-03-10 14:42:54.422 9649-9649/? D/MainActivity=-->: onResume
2020-03-10 14:42:54.480 9649-9649/? D/MainActivity=-->: onAttachedToWindow
2020-03-10 14:42:54.480 9649-9649/? D/CustomView=-->: onAttachedToWindow
2020-03-10 14:42:54.480 9649-9649/? D/CustomView=-->: onWindowVisibilityChanged
2020-03-10 14:42:54.480 9649-9649/? D/CustomView=-->: onVisibilityChanged
2020-03-10 14:42:54.488 9649-9649/? D/CustomView=-->: onMeasure
2020-03-10 14:42:54.489 9649-9649/? D/CustomView=-->: onMeasure
2020-03-10 14:42:54.511 9649-9649/? D/CustomView=-->: onMeasure
2020-03-10 14:42:54.511 9649-9649/? D/CustomView=-->: onMeasure
2020-03-10 14:42:54.511 9649-9649/? D/CustomView=-->: onSizeChanged
2020-03-10 14:42:54.511 9649-9649/? D/CustomView=-->: onLayout
2020-03-10 14:42:54.638 9649-9649/? D/CustomView=-->: onWindowFocusChanged
销毁
2020-03-10 14:43:50.009 9649-9649/com.example.testviewlifecycle D/MainActivity=-->: onPause
2020-03-10 14:43:50.019 9649-9649/com.example.testviewlifecycle D/CustomView=-->: onWindowFocusChanged
2020-03-10 14:43:50.557 9649-9649/com.example.testviewlifecycle D/CustomView=-->: onWindowVisibilityChanged
2020-03-10 14:43:50.578 9649-9649/com.example.testviewlifecycle D/MainActivity=-->: onStop
2020-03-10 14:43:50.579 9649-9649/com.example.testviewlifecycle D/CustomView=-->: onVisibilityChanged
2020-03-10 14:43:50.582 9649-9649/com.example.testviewlifecycle D/MainActivity=-->: onDestroy
2020-03-10 14:43:50.583 9649-9649/com.example.testviewlifecycle D/CustomView=-->: onDetachedFromWindow
2020-03-10 14:43:50.584 9649-9649/com.example.testviewlifecycle D/MainActivity=-->: onDetachedFromWindow
3)、android:visibility="gone"
创建
2020-03-10 14:45:03.585 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onCreate 1
2020-03-10 14:45:03.620 9921-9921/com.example.testviewlifecycle D/CustomView=-->: view Constructors 2
2020-03-10 14:45:03.620 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onFinishInflate
2020-03-10 14:45:03.621 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onCreate 2
2020-03-10 14:45:03.623 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onStart
2020-03-10 14:45:03.626 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onResume
2020-03-10 14:45:03.647 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onAttachedToWindow
2020-03-10 14:45:03.648 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onAttachedToWindow
2020-03-10 14:45:03.648 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onWindowVisibilityChanged
2020-03-10 14:45:03.648 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onVisibilityChanged
2020-03-10 14:45:03.680 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onWindowFocusChanged
销毁
2020-03-10 14:45:34.421 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onPause
2020-03-10 14:45:34.431 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onWindowFocusChanged
2020-03-10 14:45:34.978 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onWindowVisibilityChanged
2020-03-10 14:45:34.997 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onStop
2020-03-10 14:45:34.998 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onVisibilityChanged
2020-03-10 14:45:35.001 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onDestroy
2020-03-10 14:45:35.002 9921-9921/com.example.testviewlifecycle D/CustomView=-->: onDetachedFromWindow
2020-03-10 14:45:35.003 9921-9921/com.example.testviewlifecycle D/MainActivity=-->: onDetachedFromWindow
总结
1、view的创建过程是调用带有两个参数的构造函数,当然,如果该View不是在layout中定义的话,会调用一个参数的构造函数。
2、从XMl文件中inflate完成(onFinishInflate())。
3、将View加到window中(View是gone的,那么View创建生命周期也就结束)。
4、测量view的长宽(onMeasure())。
5、定位View 在父View中的位置(onLayout()),若View是invisible,则View的创建生命周期结束。
6、绘制View的content(onDraw()),只有可见的View才在window中绘制。
7、View的销毁流程和可见性没有关系。
在Activity的onCreate方法中加载View,View的onFinishInflate会被调用,继而Activity的生命周期执行到onResume方法之后View才被附着到窗口上,继而进行绘制工作,onMeasure、onSizeChanged 、onLayout、onDraw。这几个方法可能由于setVisible或onResume被调用多次,最后是Window失去焦点后的销毁阶段。
参考
Android显示框架:Android应用视图的载体View