When customizing view and ViewGroup, we often encounter int-type MEASURESPEC to represent the size of a component, which has not only the size of the component, but also the pattern of the size.
This size pattern is a little hard to understand. There are three different sizes of components in the system:
1. Precise mode (measurespec.exactly)
In this mode, the size of the value is how much, then the length or width of this component is how much.
2. Max Mode (measurespec.at_most)
This is the parent component, can give the largest space, the current component of the maximum length or width can only be so large, of course, can be smaller than this.
3. No pattern specified (measurespec.unspecified)
This means that the current component, you can use space freely, unrestricted.
There may be a lot of people who can't figure out how an int integer can represent two things (size pattern and size value), an int type We know there are 32 bits. And the pattern has three kinds, must represent three kinds of states, at least 2 bit bits. The system then uses the highest 2-bit representation mode. As shown in figure:
> 2 Recognition _ Custom components >
A maximum of two digits is 00 when the "unspecified mode" is indicated. namely Measurespec.unspecified
A maximum of two digits is 01 when it means "exact mode." namely measurespec.exactly
The maximum of two digits is 10 when the "maximum mode" is represented. namely Measurespec.at_most
A lot of people once in place Operation Head is big, in order to operate simple, so the system provides me with a Measurespec tool class.
This tool class has four methods and three constants (shown above) for our use:
This is the size and pattern we give to generate an int variable that contains both of these two messages, and here This pattern is the parameter that passes one of the three constants.
public static int Makemeasurespec (int size, int mode)
This is to get the pattern information represented in this variable and compare the resulting value to three constants.
public static int GetMode (int measurespec)
This is the value that gets the size of the dimension represented in this variable.
public static int GetSize (int measurespec)
The pattern and size inside this variable are returned to the string to make it easy to log
public static String toString (int measurespec)