Android View刷新机制



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

  1. void invalidate(boolean invalidateCache) {  
  2.           final AttachInfo ai = mAttachInfo;  
  3.           final ViewParent p = mParent;  
  4.           //noinspection PointlessBooleanExpression,ConstantConditions  
  5.           if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {  
  6.               if (p != null && ai != null && ai.mHardwareAccelerated) {  
  7.                   // fast-track for GL-enabled applications; just invalidate the whole hierarchy  
  8.                   // with a null dirty rect, which tells the ViewAncestor to redraw everything  
  9.                   p.invalidateChild(thisnull);  
  10.                   return;  
  11.               }  
  12.           }  
  13.   
  14.           if (p != null && ai != null) {  
  15.               final Rect r = ai.mTmpInvalRect;  
  16.               r.set(00, mRight - mLeft, mBottom - mTop);  
  17.               // Don't call invalidate -- we don't want to internally scroll  
  18.               // our own bounds  
  19.               p.invalidateChild(this, r);  
  20.           }  
  21.       }  
  22.   }  
  void invalidate(boolean invalidateCache) {
            final AttachInfo ai = mAttachInfo;
            final ViewParent p = mParent;
            //noinspection PointlessBooleanExpression,ConstantConditions
            if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
                if (p != null && ai != null && ai.mHardwareAccelerated) {
                    // fast-track for GL-enabled applications; just invalidate the whole hierarchy
                    // with a null dirty rect, which tells the ViewAncestor to redraw everything
                    p.invalidateChild(this, null);
                    return;
                }
            }

            if (p != null && ai != null) {
                final Rect r = ai.mTmpInvalRect;
                r.set(0, 0, mRight - mLeft, mBottom - mTop);
                // Don't call invalidate -- we don't want to internally scroll
                // our own bounds
                p.invalidateChild(this, r);
            }
        }
    }


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.             // tell our children  
  4.         if (preventRequestLayout) {  
  5.             child.assignParent(this);  
  6.         } else {  
  7.             child.mParent = this;  
  8.         }  
  9.        .....  
  10. }  
private void addViewInner(View child, int index, LayoutParams params, boolean preventRequestLayout) {
        .....
            // tell our children
        if (preventRequestLayout) {
            child.assignParent(this);
        } else {
            child.mParent = this;
        }
       .....
}

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

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

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

View.java

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

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. }  
void dispatchAttachedToWindow(AttachInfo info, int visibility) {
    super.dispatchAttachedToWindow(info, visibility);

    for (int i = 0; i < count; i++) {
        children[i].dispatchAttachedToWindow(info, visibility);
    }
}

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

ViewGroup.java

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

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

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

    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        final int[] location = attachInfo.mInvalidateChildLocation;
        // 需要刷新的子View的位置 
        location[CHILD_LEFT_INDEX] = child.mLeft;
        location[CHILD_TOP_INDEX] = child.mTop;

        // If the child is drawing an animation, we want to copy this flag onto
        // ourselves and the parent to make sure the invalidate request goes through
        final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;

        // Check whether the child that requests the invalidate is fully opaque
        final boolean isOpaque = child.isOpaque() && !drawAnimation && child.getAnimation() != null;
        // Mark the child as dirty, using the appropriate flag
        // Make sure we do not set both flags at the same time
        final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY;

        do {
            View view = null;
            if (parent instanceof View) {
                view = (View) parent;
            }

            if (drawAnimation) {
                if (view != null) {
                        view.mPrivateFlags |= DRAW_ANIMATION;
                } else if (parent instanceof ViewRoot) {
                        ((ViewRoot) parent).mIsAnimating = true;
                }
            }

                // If the parent is dirty opaque or not dirty, mark it dirty with the opaque
                // flag coming from the child that initiated the invalidate
            if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) {
                view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag;
            }

            parent = parent.invalidateChildInParent(location, dirty);
        } while (parent != null);
    }
}
 
public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) {
    if ((mPrivateFlags & DRAWN) == DRAWN) {
        if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) !=
                        FLAG_OPTIMIZE_INVALIDATE) {
            // 根据父View的位置,偏移刷新区域 
            dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, location[CHILD_TOP_INDEX] - mScrollY);

            final int left = mLeft;
            final int top = mTop;
            //计算实际可刷新区域 
            if (dirty.intersect(0, 0, mRight - left, mBottom - top) ||
                        (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) {
                mPrivateFlags &= ~DRAWING_CACHE_VALID;

                location[CHILD_LEFT_INDEX] = left;
                location[CHILD_TOP_INDEX] = top;
                return mParent;
            }
        } else {
            mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;

            location[CHILD_LEFT_INDEX] = mLeft;
            location[CHILD_TOP_INDEX] = mTop;

           dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX],
                        mBottom - location[CHILD_TOP_INDEX]);

                return mParent;
            }
        }

        return null;
}

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

ViewRoot.java

  1. public void invalidateChild(View child, Rect dirty) {  
  2.     scheduleTraversals();  
  3. }  
public void invalidateChild(View child, Rect dirty) {
    scheduleTraversals();
}

另外:

Invalidate()方法不能放在线程中,所以需要把Invalidate()方法放在Handler中。在MyThread中只需要在规定时间内发送一个Message给handler,当Handler接收到消息就调用Invalidate()方法。

postInvalidate()方法就可以放在线程中做处理,就不需要Handler。

而上面的新线程MyThread可以放在OnCreate()中开始,也可以放在OnStart()中开始。

Invalidate()方法和postInvalidate()都可以在主线程中调用而刷新视图。

Invalidate()方法在SDK中是这样描述的:Invalidatethe whole view. If the view is visible, onDraw(Canvas) will be called at somepoint in the future. This must be called from a UI thread. To call from anon-UI thread, call postInvalidate().  当Invalidate()被调用的时候,View的OnDraw()就会被调用,Invalidate()必须是在UI线程中被调用,如果在新线程中更新视图的就调用postInvalidate()。

发布了6 篇原创文章 · 获赞 44 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/sinat_29255093/article/details/52572190