View work progress :Measure

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


In onMeasure’s code,there is just one method invoked:

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 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

A software developer

Leave a Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.