Android onMeasure, Measure, measureChild, and measureChildren

Source: Internet
Author: User

Android onMeasure, Measure, measureChild, and measureChildren

Definition in View. java:

Public final void measure (int widthMeasureSpec, int heightMeasureSpec ){

...

OnMeasure

...

}

Protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec ){

SetMeasuredDimension (getDefaultSize (getSuggestedMinimumWidth (), widthMeasureSpec ),

GetDefaultSize (getSuggestedMinimumHeight (), heightMeasureSpec ));

}

Public static int getDefaultSize (int size, int measureSpec) {int result = size; int specMode = MeasureSpec. getMode (measureSpec); int specSize = MeasureSpec. getSize (measureSpec); switch (specMode) {case MeasureSpec. UNSPECIFIED: // UNSPECIFIED result = size; break; case MeasureSpec. AT_MOST: // up to case MeasureSpec. EXACTLY: // exact result = specSize; break;} return result ;}

In ViewGroup:

   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);            }        }    }

   protected void measureChild(View child, int parentWidthMeasureSpec,            int parentHeightMeasureSpec) {        final LayoutParams lp = child.getLayoutParams();        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,                mPaddingLeft + mPaddingRight, lp.width);        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,                mPaddingTop + mPaddingBottom, lp.height);        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);    }

 protected void measureChildWithMargins(View child,            int parentWidthMeasureSpec, int widthUsed,            int parentHeightMeasureSpec, int heightUsed) {        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin                        + widthUsed, lp.width);        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin                        + heightUsed, lp.height);        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);    }

Note:

1. measure is a final modifier and cannot be overwritten.

In an external call, view. measure (int wSpec, int hSpec) is called directly ).

OnMeasure is called in measure.

When customizing a view, override onMeasure.


2. MeasureSpec this is a combination of mode and size. We don't need to worry about it.

When measuring, you can call MeasureSpec. getSize | getMode to obtain the corresponding size and mode.

Then, use MeasureSpec. makeMeasureSpec (size, mode); To create the MeasureSpec object.

So how does mode come from? It is determined by the layoutWith | height parameter when the custom view is used. Therefore, you cannot create one by yourself.

You can specify the size or use measureSpec. getSize directly.


3. For a View, note the following when rewriting onMeasure:

If wrap_content is used when a custom view is used. In onMeasure, setMeasuredDimension is called,

To specify the width and height of the view. If fill_parent or a specific dp value is used. Use super. onMeasure directly.


4. If it is a ViewGroup, note the following when rewriting onMeasure:

First, use the above two methods to measure the width and height.

Then, you need to measure the width and height of the sub-View.

The following methods are used to measure the sub-view:

GetChildAt (int index). You can get the child view on the index.

GetChildCount is used to obtain the number of sub-views and traverse the sub-views.

Next, subView. measure (int wSpec, int hSpec); // use the measurement method of the Child view.

You can also call the method of viewGroup's measurement sub-view:

// A sub-view with the padding value of viewGroup added internally.

MeasureChild (subView, int wSpec, int hSpec );

// The measureChild method is called internally for how wide and high all sub-views are.

MeasureChildren (int wSpec, int hSpec );

// A sub-view with multiple widths and heights. the padding value, margin value, and incoming width and height wUsed and hUsed are added to the viewGroup.

MeasureChildWithMargins (subView, intwSpec, int wUsed, int hSpec, int hUsed );


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.