博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
View分析
阅读量:6869 次
发布时间:2019-06-26

本文共 5404 字,大约阅读时间需要 18 分钟。

在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 onto13         // ourselves and the parent to make sure the invalidate request goes through14         final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION;15 16         // Check whether the child that requests the invalidate is fully opaque17         final boolean isOpaque = child.isOpaque() && !drawAnimation && child.getAnimation() != null;18 19         // Mark the child as dirty, using the appropriate flag20         // Make sure we do not set both flags at the same time21         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 opaque38                 // flag coming from the child that initiated the invalidate39             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 }

 

转载于:https://www.cnblogs.com/mjblogs/p/3899365.html

你可能感兴趣的文章
php下使用PDO创建sqlite3数据库
查看>>
Istio技术与实践6:Istio如何为服务提供安全防护能力
查看>>
大型架构及配置技术Docker
查看>>
ISTP的重要作用
查看>>
GitHub---(使用SSH方式)
查看>>
大规模生产计划技术管理核心理念
查看>>
阿里云HBase推出全新X-Pack服务 定义HBase云服务新标准
查看>>
驼峰设计 PPT美化
查看>>
hcl DHCP与snooping的实践操作 中有个问题
查看>>
Python Python 正则 取中括号值
查看>>
springMVC-接收数据-参数绑定
查看>>
OOM问题定位方法
查看>>
hadoop day 4
查看>>
SCVMM 2012 SP1 Beta 体验(一)安装准备
查看>>
Lync Server 2013功能部署系列之七 实现Lync Server 与Exchange Server集成的功能
查看>>
老王学Linux-NFS
查看>>
这一次,我们用最详细的方式解析Android消息机制的源码
查看>>
bash之条件判断语句及循环语句
查看>>
shell-5:case比if(elif else)方便
查看>>
R基础数据类型2DAY
查看>>