该方法用于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