The onmeasure method is called when the parent element of the control is about to place its child control. It will ask a question: "How much do you want to use ?", Then two parameters -- widthmeasurespec and heightmeasurespec are input.
They indicate the space available for the control and metadata about the space description.
A better way than returning a result is to pass the view height and width to the setmeasureddimension method.
[Click to download the scientific Internet software (go to YouTube and Facebook and enjoy the Google service )]
The following code snippet shows how to override onmeasure. note that the local null method is called to calculate the height and width. they will translate the widthheightspec and heightmeasurespec values and calculate the appropriate height and width values.
Java code:
- @ Override
- Protected void onmeasure (INT widthmeasurespec, int heightmeasurespec ){
- Int measuredheight = measureheight (heightmeasurespec );
- Int measuredwidth = measurewidth (widthmeasurespec );
- Setmeasureddimension (measuredheight, measuredwidth );
- }
- Private int measureheight (INT measurespec ){
- // Return measured widget height.
- }
- Private int measurewidth (INT measurespec ){
- // Return measured widget width.
- }
Copy code
Border parameters -- widthmeasurespec and heightmeasurespec. The reason for efficiency is passed in as an integer.
Measurespec encapsulates the layout requirements that the parent layout passes to the Child layout. Each measurespec represents a set of width and height requirements. A measurespec consists of the size and mode.
It has three modes:
Unspecified (not specified). The parent element does not apply any constraints to the self-element, and the child element can obtain any desired size;
Exactly (complete): the parent element determines the exact size of the Self-element. The child element is limited to the given boundary and its size is ignored;
At_most (at most), the child element can reach the specified size at most.
It has three common functions:
1. Static int getmode (INT measurespec): extract the mode based on the provided measured value (Format) (one of the preceding three modes)
2. Static int getsize (INT measurespec): extract the size value based on the provided measured value (Format) (this size is what we usually call the size)
3. Static int makemeasurespec (INT size, int mode): Create a measurement value based on the provided size value and mode (Format)
This class is usually called in the onmeasure method of the view component, but there are a few exceptions.
Before using them, the first thing to do is to use the static methods getmode and getsize of the measurespec class for translation, as shown in the following snippet:
Java code:
- Int specmode = measurespec. getmode (measurespec );
- Int specsize = measurespec. getsize (measurespec );
Copy code
Depending on the specmode value, if it is at_most, specsize represents the maximum available space; if it is exactly, specsize represents the precise size; if it is unspecified, for the control size, it has no reference significance.
When the measurement size is marked as exact, the parent element will stick to the view in a specified precise size area. When the parent element asks the child element for more space, the at_most indicator will give me the greatest range. In many cases, the values you get are the same.
In either case, you must absolutely handle these restrictions. In some cases, it may return dimensions that exceed these limits. In this case, you can ask the parent element to choose how to treat the view that exceeds the limit, using techniques such as cropping or scrolling.
The following framework code provides a typical implementation for processing view measurements:
Java code:
- @ Override
- Protected 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;
- }