@linux1s1s
2016-11-07T19:50:37.000000Z
字数 5491
阅读 2378
AndroidRefine
2016-11
系列博文:
Android 生命周期 - Activity
Android 生命周期 - Service
Android 生命周期 - BroadcastReceiver
Android 生命周期 - Fragment
Android 生命周期 - View
View中的常见方法简单介绍如下:
onFinishInflate()
: 该方法当View及其子View从XML文件中加载完成后会被调用onMeasure(int, int)
:该方法在计算当前View及其所有子View尺寸大小需求时会被调用。onLayout(boolean, int, int, int, int)
:该方法在当前View需要为其子View分配尺寸和位置时会被调用。onSizeChanged(int, int, int, int)
:该方法在当前View尺寸变化时被调用。onDraw(android.graphics.Canvas)
:该方法在当前View需要呈现其内容时被调用。onFocusChanged(boolean, int, android.graphics.Rect)
:该方法在当前View获得或失去焦点时被调用。onWindowFocusChanged(boolean)
:该方法在包含当前View的window获得或失去焦点时被调用。onAttachedToWindow()
:该方法在当前View被附到一个window上时被调用。onDetachedFromWindow()
:该方法在当前View从一个window上分离时被调用。onVisibilityChanged(View, int)
:该方法在当前View或其祖先的可见性改变时被调用。onWindowVisibilityChanged(int)
:该方法在包含当前View的window可见性改变时被调用。
public class BCView extends View {
@Override
protected void onVisibilityChanged(View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
Log.i(MainActivity.TAG, "BCView onVisibilityChanged(View changedView, int visibility) changedView = " + visibility);
}
public BCView(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(MainActivity.TAG, "BCView BCView(Context context, AttributeSet attrs)");
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Log.i(MainActivity.TAG, "BCView onAttachedToWindow()");
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Log.i(MainActivity.TAG, "BCView onMeasure(int widthMeasureSpec, int heightMeasureSpec) widthMeasureSpec = " + widthMeasureSpec + " heightMeasureSpec = " + heightMeasureSpec);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
Log.i(MainActivity.TAG, "BCView onSizeChanged(int w, int h, int oldw, int oldh)");
}
@Override
protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
Log.i(MainActivity.TAG, "BCView onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)");
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
Log.i(MainActivity.TAG, "BCView onFinishInflate()");
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
Log.i(MainActivity.TAG, "BCView onLayout(boolean changed, int left, int top, int right, int bottom) changed = " + changed + " left = " + left + " top = " + top + " right = " + right + " bottom = " + bottom);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.i(MainActivity.TAG, "BCView onDraw(Canvas canvas)");
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
super.onWindowFocusChanged(hasWindowFocus);
Log.i(MainActivity.TAG, "BCView onWindowFocusChanged(boolean hasWindowFocus) hasWindowFocus = " + hasWindowFocus);
}
@Override
protected void onWindowVisibilityChanged(int visibility) {
super.onWindowVisibilityChanged(visibility);
Log.i(MainActivity.TAG, "BCView onWindowVisibilityChanged(int visibility) visibility = " + visibility);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
Log.i(MainActivity.TAG, "BCView onDetachedFromWindow()");
}
}
public class MainActivity extends AppCompatActivity {
public static final String TAG = "mutex";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "MainActivity onCreate()");
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="fresco.mutex.com.lifecycle.MainActivity">
<fresco.mutex.com.lifecycle.BCView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:background="@android:color/holo_blue_light" />
</RelativeLayout>
启动以后,查看Log日志
从启动到View显示完整的生命周期如上所示,可以小结出大的轮廓如下:
Constructors()构造器 ---->> onFinishInflate() ---->> onAttachedToWindow --->> onMeasure ---->> onLayout() --->> onDraw()
接着Back回退
onWindowFocusChanged --->> onWindowVisibilityChanged --->> onDetachedFromWindow
接下来将上面的BCView属性设置invisible如下:
<fresco.mutex.com.lifecycle.BCView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@id/activity_main_send"
android:layout_centerInParent="true"
android:visibility="invisible"
android:background="@android:color/holo_blue_light" />
onVisibilityChanged()
方法,后面缺少了onDraw()
方法。最后,我们将BCView的属性设置为Gone如下:
<fresco.mutex.com.lifecycle.BCView
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_below="@id/activity_main_send"
android:layout_centerInParent="true"
android:visibility="gone"
android:background="@android:color/holo_blue_light" />
onDraw()
方法,同时onMeasure()
以及onLayout()
方法也没有了。
- View默认为可见的,否则先调用
onVisibilityChanged()
,但是此时该View的长宽以及位置等信息都不知道。- 如果View是
invisible
,则不会回调onDraw()
方法- 如果View是
gone
,则不会回调onMeasure(),onLayout(),onDraw()
方法- 不管View是什么状态,总是会回调
onAttachedToWindow()
以及onDetachedFromWindow()
方法- View的销毁流程和可见性没有关系
- [onVisibilityChanged()可选] --> 构造器View() --> onFinishInflate() --> onAttachedToWindow() --> onMeasure() --> onSizeChanged() --> onLayout() --> onDraw() --> onDetackedFromWindow()