码迷,mamicode.com
首页 > 移动开发 > 详细

Android学习笔记之View(二)

时间:2016-02-29 14:21:11      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

View加载的流程之测量:rootView调用measure()→onMeasure();

measure()是final方法,表明Android不想让开发者去修改measure的框架,开发者可以onMeasure方法。

来看一下measure的代码:

  • public?final?void?measure(int?widthMeasureSpec,?int?heightMeasureSpec)?{??
  • ????if?((mPrivateFlags?&?FORCE_LAYOUT)?==?FORCE_LAYOUT?||??
  • ????????????widthMeasureSpec?!=?mOldWidthMeasureSpec?||??
  • ????????????heightMeasureSpec?!=?mOldHeightMeasureSpec)?{??
  • ????????mPrivateFlags?&=?~MEASURED_DIMENSION_SET;??
  • ????????if?(ViewDebug.TRACE_HIERARCHY)?{??
  • ????????????ViewDebug.trace(this,?ViewDebug.HierarchyTraceType.ON_MEASURE);??
  • ????????}??
  • ????????onMeasure(widthMeasureSpec,?heightMeasureSpec);??
  • ????????if?((mPrivateFlags?&?MEASURED_DIMENSION_SET)?!=?MEASURED_DIMENSION_SET)?{??
  • ????????????throw?new?IllegalStateException("onMeasure()?did?not?set?the"??
  • ????????????????????+?"?measured?dimension?by?calling"??
  • ????????????????????+?"?setMeasuredDimension()");??
  • ????????}??
  • ????????mPrivateFlags?|=?LAYOUT_REQUIRED;??
  • ????}??
  • ????mOldWidthMeasureSpec?=?widthMeasureSpec;??
  • ????mOldHeightMeasureSpec?=?heightMeasureSpec;??
  • }??

    ? ?

? ?

一个界面的展示可能会涉及到很多次发measure,因为一个视图往往包含多个子视图,每个视图都需要经历一次measure过程。ViewGroup中定义了measureChildren()方法来测量子视图,下面是measureChildren():

? ?

  • protected?void?measureChildren(int?widthMeasureSpec,?int?heightMeasureSpec)?{??
  • ????final?int?size?=?mChildrenCount;??
  • ????final?View[]?children?=?mChildren;??
  • ????for?(int?i?=?0;?i?<?size;?++i)?{??
  • ????????final?View?child?=?children[i];??
  • ????????if?((child.mViewFlags?&?VISIBILITY_MASK)?!=?GONE)?{??
  • ????????????measureChild(child,?widthMeasureSpec,?heightMeasureSpec);??
  • ????????}??
  • ????}??
  • }??

? ?

View加载的第二步: onLayout,这和measure差不多,都是由rootView调用layout()→onLayout()。

? ?

? ?

Android学习笔记之View(二)

标签:

原文地址:http://www.cnblogs.com/yxx123/p/5227172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!