zoukankan      html  css  js  c++  java
  • View分析

    在Android的布局体系中,父View负责刷新、布局显示子View;而当子View需要刷新时,则是通知父View来完成。这种处理逻辑在View的代码中明确的表现出来: 

     1 public void invalidate() {
     2     final ViewParent p = mParent;
     3     final AttachInfo ai = mAttachInfo;
     4     if (p != null && ai != null) {
     5         final Rect r = ai.mTmpInvalRect;
     6 
     7         // 设置刷新区域为自己的尺寸 
     8         r.set(0, 0, mRight - mLeft, mBottom - mTop);
     9         p.invalidateChild(this, r);
    10     }
    11 }

     子View调用invalidate时,首先找到自己父View(View的成员变量mParent记录自己的父View),然后将AttachInfo中保存的信息告诉父View刷新自己。 

    View的父子关系的建立分为两种情况:

    1) View加入ViewGroup中

     1 private void addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout) {
     2 
     3         .....
     4 
     5             // tell our children
     6         if (preventRequestLayout) {
     7             child.assignParent(this);
     8         } else {
     9             child.mParent = this;
    10         }
    11 
    12        .....
    13 
    14 } 

    2)DecorView注册给WindowManagerImpl时,产生一个ViewRoot作为其父View。

    1 public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView){
    2 
    3     .....
    4 
    5     view.assignParent(this);
    6 
    7     ....
    8 
    9 }

      AttachInfo是在View第一次attach到Window时,ViewRoot传给自己的子View的。这个AttachInfo之后,会顺着布局体系一直传递到最底层的View。

    View.java 

    1 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
    2     mAttachInfo = info;
    3 
    4     .....
    5 }

     ViewGroup.java

    1 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
    2     super.dispatchAttachedToWindow(info, visibility);
    3 
    4     for (int i = 0; i < count; i++) {
    5         children[i].dispatchAttachedToWindow(info, visibility);
    6     }
    7 }

     并且在新的View被加入ViewGroup时,也会将该AttachInfo传给加入的View

    ViewGroup.java

    1 private void addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout) {
    2 
    3     child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK));
    4 
    5 }

     到这里明白了mParent与AttachInfo代表的意义,可以继续刷新过程的分析。

     在invalidate中,调用父View的invalidateChild,这是一个从第向上回溯的过程,每一层的父View都将自己的显示区域与传入的刷新Rect做交集。 

     1 public final void invalidateChild(View child, final Rect dirty) {
     2     ViewParent parent = this;
     3 
     4     final AttachInfo attachInfo = mAttachInfo;
     5     if (attachInfo != null) {
     6         final int[] location = attachInfo.mInvalidateChildLocation;
     7 
     8         // 需要刷新的子View的位置 
     9         location[CHILD_LEFT_INDEX] = child.mLeft;
    10         location[CHILD_TOP_INDEX] = child.mTop;
    11 
    12         // If the child is drawing an animation, we want to copy this flag onto
    13         // ourselves and the parent to make sure the invalidate request goes through
    14         final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;
    15 
    16         // Check whether the child that requests the invalidate is fully opaque
    17         final boolean isOpaque = child.isOpaque() && !drawAnimation && child.getAnimation() != null;
    18 
    19         // Mark the child as dirty, using the appropriate flag
    20         // Make sure we do not set both flags at the same time
    21         final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;
    22 
    23         do {
    24             View view = null;
    25             if (parent instanceof View) {
    26                 view = (View) parent;
    27             }
    28 
    29             if (drawAnimation) {
    30                 if (view != null) {
    31                         view.mPrivateFlags |= DRAW_ANIMATION;
    32                 } else if (parent instanceof ViewRoot) {
    33                         ((ViewRoot) parent).mIsAnimating = true;
    34                 }
    35             }
    36 
    37                 // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
    38                 // flag coming from the child that initiated the invalidate
    39             if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
    40                 view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
    41             }
    42 
    43             parent = parent.invalidateChildInParent(location, dirty);
    44         } while (parent != null);
    45     }
    46 }
    47 
    48  
    49 
    50 public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
    51     if ((mPrivateFlags & DRAWN) == DRAWN) {
    52         if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
    53                         FLAG_OPTIMIZE_INVALIDATE) {
    54 
    55             // 根据父View的位置,偏移刷新区域 
    56             dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, location[CHILD_TOP_INDEX] - mScrollY);
    57 
    58             final int left = mLeft;
    59             final int top = mTop;
    60 
    61             //计算实际可刷新区域 
    62             if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||
    63                         (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
    64                 mPrivateFlags &= ~DRAWING_CACHE_VALID;
    65 
    66                 location[CHILD_LEFT_INDEX] = left;
    67                 location[CHILD_TOP_INDEX] = top;
    68                 return mParent;
    69             }
    70         } else {
    71             mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
    72 
    73             location[CHILD_LEFT_INDEX] = mLeft;
    74             location[CHILD_TOP_INDEX] = mTop;
    75 
    76            dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],
    77                         mBottom - location[CHILD_TOP_INDEX]);
    78 
    79                 return mParent;
    80             }
    81         }
    82 
    83         return null;
    84 }

    这个向上回溯的过程直到ViewRoot那里结束,由ViewRoot对这个最终的刷新区域做刷新。

    ViewRoot.java

    1 public void invalidateChild(View child, Rect dirty) {
    2 
    3     scheduleTraversals();
    4 
    5 }
  • 相关阅读:
    VC ODBC使用总结
    AppBaseJs 类库 网上常用的javascript函数及其他js类库写的
    VC 6.0 下搭建 wxWidgets 开发环境
    ASP.NET页面周期学习笔记之一
    自己动手写三层代码生成器学习总结
    锋利的JQuery学习笔记之JQueryAjax的应用
    缓存(Cache)学习笔记
    C#基础与常用数据结构学习笔记
    ASP.NET网络安全简单防护公开课视频学习笔记
    黑马公开课——运行原理与GC学习笔记
  • 原文地址:https://www.cnblogs.com/mjblogs/p/3899365.html
Copyright © 2011-2022 走看看