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