OnMeasure method of Android

Source: Internet
Author: User

In Android development, when the Android native control cannot meet our needs, we need to customize the View. To draw a View on the screen, you must first pass through the measure (calculation) and layout (layout ).

When can I call the onMeasure method?
When the parent control of the Child View is placed in this View, the parent control will pass two parameters to View -- widthMeasureSpec and heightMeasureSpec. The two parameters are int data that can be obtained by View in combination with the width, height, and mode values. You can use int mode = MeasureSpec. getMode (widthMeasureSpec) to obtain the mode, and use int size = MeasureSpec. getSize (widthMeasureSpec) to obtain the size.

There are three scenarios in which the values are

MeasureSpec. UNSPECIFIED, MeasureSpec. EXACTLY, MeasureSpec. AT_MOST.


MeasureSpec. EXACTLY is the exact size. When we specify the layout_width or layout_height of the control as a specific value, as shown in
Andorid: layout_width = "50dip", or "FILL_PARENT", all of which are precise sizes when the control size is determined.

MeasureSpec. AT_MOST is the maximum size. When the layout_width or layout_height of the control is set to WRAP_CONTENT, the control Size usually changes with the control's sub-spaces or content, in this case, the widget size must not exceed the maximum size allowed by the parent widget. Therefore, the mode is AT_MOST, and size provides the maximum size allowed by the parent control.

MeasureSpec. UNSPECIFIED is an UNSPECIFIED size. In this case, the parent control is generally AdapterView, which is passed in through the measure method.

You can call the setMeasuredDimenson method to pass in the View height and width, set the actual size of the Child View, and tell the parent control how much space to place the child View.

The typical onMeasure Implementation of View in the framework is as follows:

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int measuredHeight = measureHeight(heightMeasureSpec);int measuredWidth = measureWidth(widthMeasureSpec);setMeasuredDimension(measuredHeight, measuredWidth);}private int measureHeight(int measureSpec) {int specMode = MeasureSpec.getMode(measureSpec);int specSize = MeasureSpec.getSize(measureSpec);// Default size if no limits are specified.int result = 500;if (specMode == MeasureSpec.AT_MOST) {// Calculate the ideal size of your// control within this maximum size.// If your control fills the available// space return the outer bound.result = specSize;} else if (specMode == MeasureSpec.EXACTLY) {// If your control can fit within these bounds return that// value.result = specSize;}return result;}private int measureWidth(int measureSpec) {int specMode = MeasureSpec.getMode(measureSpec);int specSize = MeasureSpec.getSize(measureSpec);// Default size if no limits are specified.int result = 500;if (specMode == MeasureSpec.AT_MOST) {// Calculate the ideal size of your control// within this maximum size.// If your control fills the available space// return the outer bound.result = specSize;}else if (specMode == MeasureSpec.EXACTLY) {// If your control can fit within these bounds return that// value.result = specSize;}return result;}

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.