About the DecorView and MeasureSpec in Android View

View and DecorView

When Activity created,will add DecorView to window,and create ViewRootImp object at the same time,building associated with DecorView and ViewRootImp object.

As shown in the following code:

The draw of view is started from ViewRoot’s performTraversals method,through measure,layout,draw three flows to draw a view,about these three flow you can see the below blog
View:Measure
View:Layout and Draw

This three flow can summarize in below pic
fllow1

DecorView’s layout

DecorView is our top View, general contains a general LinearLayout, this layout assemble from top TitleView and bottom ContentView two parts.

As below pic:
fllow2
Now,you can understand why in Activity,we call setContentView to set layout,yes,we actual is set the content of ContentView.

About MeasureSpace

What MeasureSpec is?

MeasureSpec is a 32 bit int value, consists of two parts,first two bits mean SpecMode other 30 bits mean SpecSize. Why do not simply set into two values?Because it can reduce the number of variables, so as to achieve the purpose of reduce resource.

MeasureSpec’s main function is to support size information in onMeasure process,the mode and size,to determine how to measure in the end.

MeasureSpec’s SpecMode

There are three SpecMode
UNSPECIFIED
The parent container not restrictions size of children view,commonly used in internal system,show as a state.

EXACTLY
At this time the size of the View is the value of SpecSize, corresponding match_parenta and specific values two mode in layoutparams.

AT_MOST
View can’t bigger than SpecSize,corresponding wrap_content in layoutparams.

The relationship of MeasureSpec and LayoutParams

MeasureSpec is generated through its own LayoutParams and the parent container’s MeasureSpec.

Cause DecorView is the top View,So the process of its MeasureSpec with normal View is slightly different, is according to the size of the screen and its layoutparams.

For normal View,when get it’s SpecMesure,first will get mode and size from parent’s MessureSpec, then depend on own layoutparams to decide the mode and size in it’s MeasureSpec,at last get it’s own MeasureSpec.

Detail show in the below chart

fllow3

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