[关闭]
@linux1s1s 2016-11-07T19:50:37.000000Z 字数 5491 阅读 2378

Android 生命周期 - View

AndroidRefine 2016-11


系列博文:

Android 生命周期 - Activity
Android 生命周期 - Service
Android 生命周期 - BroadcastReceiver
Android 生命周期 - Fragment
Android 生命周期 - View

View常见方法说明

View中的常见方法简单介绍如下:

Java代码

BCView.java

  1. public class BCView extends View {
  2. @Override
  3. protected void onVisibilityChanged(View changedView, int visibility) {
  4. super.onVisibilityChanged(changedView, visibility);
  5. Log.i(MainActivity.TAG, "BCView onVisibilityChanged(View changedView, int visibility) changedView = " + visibility);
  6. }
  7. public BCView(Context context, AttributeSet attrs) {
  8. super(context, attrs);
  9. Log.i(MainActivity.TAG, "BCView BCView(Context context, AttributeSet attrs)");
  10. }
  11. @Override
  12. protected void onAttachedToWindow() {
  13. super.onAttachedToWindow();
  14. Log.i(MainActivity.TAG, "BCView onAttachedToWindow()");
  15. }
  16. @Override
  17. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  18. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  19. Log.i(MainActivity.TAG, "BCView onMeasure(int widthMeasureSpec, int heightMeasureSpec) widthMeasureSpec = " + widthMeasureSpec + " heightMeasureSpec = " + heightMeasureSpec);
  20. }
  21. @Override
  22. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  23. super.onSizeChanged(w, h, oldw, oldh);
  24. Log.i(MainActivity.TAG, "BCView onSizeChanged(int w, int h, int oldw, int oldh)");
  25. }
  26. @Override
  27. protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
  28. super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
  29. Log.i(MainActivity.TAG, "BCView onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)");
  30. }
  31. @Override
  32. protected void onFinishInflate() {
  33. super.onFinishInflate();
  34. Log.i(MainActivity.TAG, "BCView onFinishInflate()");
  35. }
  36. @Override
  37. protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  38. super.onLayout(changed, left, top, right, bottom);
  39. 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);
  40. }
  41. @Override
  42. protected void onDraw(Canvas canvas) {
  43. super.onDraw(canvas);
  44. Log.i(MainActivity.TAG, "BCView onDraw(Canvas canvas)");
  45. }
  46. @Override
  47. public void onWindowFocusChanged(boolean hasWindowFocus) {
  48. super.onWindowFocusChanged(hasWindowFocus);
  49. Log.i(MainActivity.TAG, "BCView onWindowFocusChanged(boolean hasWindowFocus) hasWindowFocus = " + hasWindowFocus);
  50. }
  51. @Override
  52. protected void onWindowVisibilityChanged(int visibility) {
  53. super.onWindowVisibilityChanged(visibility);
  54. Log.i(MainActivity.TAG, "BCView onWindowVisibilityChanged(int visibility) visibility = " + visibility);
  55. }
  56. @Override
  57. protected void onDetachedFromWindow() {
  58. super.onDetachedFromWindow();
  59. Log.i(MainActivity.TAG, "BCView onDetachedFromWindow()");
  60. }
  61. }

MainActivity.java

  1. public class MainActivity extends AppCompatActivity {
  2. public static final String TAG = "mutex";
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. Log.i(TAG, "MainActivity onCreate()");
  8. }
  9. }

资源文件

activity.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:id="@+id/activity_main"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:paddingBottom="@dimen/activity_vertical_margin"
  8. android:paddingLeft="@dimen/activity_horizontal_margin"
  9. android:paddingRight="@dimen/activity_horizontal_margin"
  10. android:paddingTop="@dimen/activity_vertical_margin"
  11. tools:context="fresco.mutex.com.lifecycle.MainActivity">
  12. <fresco.mutex.com.lifecycle.BCView
  13. android:layout_width="80dp"
  14. android:layout_height="80dp"
  15. android:layout_centerInParent="true"
  16. android:background="@android:color/holo_blue_light" />
  17. </RelativeLayout>

启动以后,查看Log日志

Log日志

此处输入图片的描述

onWindowFocusChanged --->> onWindowVisibilityChanged --->> onDetachedFromWindow

接下来将上面的BCView属性设置invisible如下:

  1. <fresco.mutex.com.lifecycle.BCView
  2. android:layout_width="80dp"
  3. android:layout_height="80dp"
  4. android:layout_below="@id/activity_main_send"
  5. android:layout_centerInParent="true"
  6. android:visibility="invisible"
  7. android:background="@android:color/holo_blue_light" />

最后,我们将BCView的属性设置为Gone如下:

  1. <fresco.mutex.com.lifecycle.BCView
  2. android:layout_width="80dp"
  3. android:layout_height="80dp"
  4. android:layout_below="@id/activity_main_send"
  5. android:layout_centerInParent="true"
  6. android:visibility="gone"
  7. android:background="@android:color/holo_blue_light" />

小结

  • View默认为可见的,否则先调用onVisibilityChanged(),但是此时该View的长宽以及位置等信息都不知道。
  • 如果View是invisible,则不会回调onDraw()方法
  • 如果View是gone,则不会回调onMeasure(),onLayout(),onDraw()方法
  • 不管View是什么状态,总是会回调onAttachedToWindow()以及onDetachedFromWindow()方法
  • View的销毁流程和可见性没有关系
  • [onVisibilityChanged()可选] --> 构造器View() --> onFinishInflate() --> onAttachedToWindow() --> onMeasure() --> onSizeChanged() --> onLayout() --> onDraw() --> onDetackedFromWindow()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注