当前位置:首页 > 未命名 > 正文内容

View work progress :Measure

u3blog8年前 (2016-03-05)未命名194

view's work flow

  • measure decide the size of view
  • layout layout the view in screen
  • draw draw the view

measure's kind

As we know,there are two kind of view,view and viewgroup. view is like the TextView, ImageView, viewgroup have child view,such as layout. Measure is different by different kind of view.

View's measure

First invokes a final method called measure inside the view,because this method is final,we can't override,but it's invokes onMeasure methods,we can override it. At first look a flow pic onMeasure流程图 In onMeasure's code,there is just one method invoked:
setMeasureDimension(getDefaultSize(getSuggestedMiniWidth(),widthMeasureSpec),
getDefaultSize(getSuggestedMiniHeight(),heightMeasureSpec));
setMeasureDimension decide size with spaceMode and spacSize,this two value come from MeasureSpec. Invoke getSuggestedMiniWidth() method,in this method,according to whether there is background to determine minimal size of the View,if not ,minimal size is same with default value,if have,same with the background minimal value. How to get a default value? Is different by different kind of view,some view have default,some not,as the sample in the book,ShapeDrawable not have it, but BitmapDrawable have.

ViewGroup's measure

In ViewGroup,not override onMeasure method,but have a measureChildren method,in this method will measure every child view. ViewGroup not implement onMeasure method because different viewgroup have different child layout.

Measure process and life cycle of the activity is not synchronized

Cause they are not synchronized,so we can't get high or width in activity after measure finish ,there are four way to resolve it 1.override onWindowFocusChanged method,in this method measure is finished 2.use view.post(runnable) method 3.ViewTreeObserver's callback onGlobalLayout() method 4.get in View.measure,but it's limit by layoutparam,no use when the mode is match_parent

扫描二维码推送至手机访问。

版权声明:本文由u3blog发布,如需转载请注明出处。

本文链接:https://u3blog.xyz/?id=253

分享给朋友:

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。