Android View的生命周期(1)

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

最后

下面是辛苦给大家整理的学习路线

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

omView=–>: view Constructors 2

最后

下面是辛苦给大家整理的学习路线

[外链图片转存中…(img-MoNxOCb9-1714298729638)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 21
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值