Android View measure (ii) Customizing UI controls measure related

Source: Internet
Author: User

This article simulates three roles: Android architect-Fulamatu, Android control development engineer-small black, Android development engineer-Small white, below three roles to analyze the measure process in different angles.


Xiao Fu is responsible for sharing :

    • The essence of Measure
    • Measure code Flow
    • Onmeasure Method and Measurespec
    • Ask questions

Little Black is responsible for sharing :

    • Layout Control Development Overwrite measure example-ok
    • Starting with an anomaly that's been encountered.
    • When do I need to overwrite onmeaure? -OK
    • difference between view.getwidth and View.getmeasurewidth-ok


Android Control Development Engineer-Little black share
First, layout control development in the overwrite measure example can be seen in the previous example written "Overwrite onmeaure for Measure operation", Android provides the layout framelaout, LinearLayout, Relativelayout are subclasses of ViewGroup, and you can refer to the ONMEAUSRE implementations of these classes to see how the Android framework is handled.
second, from the encounter of an anomaly ,If you use return in Onmeasure, the setmeasureddimension (width, height) is not performed, and a similar operation occurs with the following exception
Java.lang.IllegalStateException:onMeasure () did not set the measured dimension by calling Setmeasureddimension ()

-Ready to fill-
measure methods provided by ViewGroupMeasurechildren ()-The function uses a for () Loop call Measurechild () to measure each sub-view measurechild ()- Measure operation for each sub-view measurechildwidthmargins ()-The only difference between this function and measurechild is that measure consider margin and padding as part of the child view size
When do I need to overwrite onmeaure? As long as it is a custom control and viewgroup needs to overwrite onmeasure to specify its measurement view size rules, Androd provides Framelaout, LinearLayout, Relativelayout and so are viewgroup subclasses and all overwrite the Onmeasure method, the custom layout can first consider the subclass of these classes first, it may be simpler.
Four, View.getwidth () and view.getmeasuredwidth () difference from the name can be seen that both methods are to obtain the width, the corresponding also has the method of acquiring height, but the question is what is the difference between the two? The following directly from the source to see where these values are from, so as to learn the difference between the two.
First look at the View.java in the Getmeasuredwidth method, the source code is as follows:
public class View implements Drawable.callback, Drawable.callback2, Keyevent.callback, Accessibilityeventsource { /** * Like {@link #getMeasuredWidthAndState ()}, but only returns the * raw width component (which is the result I     S masked by * {@link #MEASURED_SIZE_MASK}).     * * @return The raw measured width of this view. */Public final int getmeasuredwidth () {//return mmeasuredwidth with the latter and clean off other switches get really measure size return Mmeasuredw    Idth & Measured_size_mask; }/** * <p>this Mehod must be called by {@link #onMeasure (int, int.)} to store the * measured width and me asured height. Failing to does so would trigger an * exception at measurement time.</p> * * @param measuredwidth the Measu  Red width of this view.     May is a complex * bit mask as defined by {@link #MEASURED_SIZE_MASK} and * {@link #MEASURED_STATE_TOO_SMALL}.  * @param measuredheight The measured height of this view. May be a complex * biT mask as defined by {@link #MEASURED_SIZE_MASK} and * {@link #MEASURED_STATE_TOO_SMALL}. */protected final void setmeasureddimension (int measuredwidth, int measuredheight) {//usually called in onmeasure, incoming measured view        Figure width and height mmeasuredwidth = measuredwidth;        Mmeasuredheight = Measuredheight;    Mprivateflags |= Measured_dimension_set; }/** * Bits of {@link #getMeasuredWidthAndState ()} and * {@link #getMeasuredWidthAndState ()} that provide the     Actual measured size. *///Measurespec in mode or occupy int type in the first few public static final int measured_size_mask = 0X00FFFFFF;}

From the above two methods we can see that the value of Getmeasuredwidth is obtained from the mmeasuredwidth variable, This variable is initialized only in the View.setmeasureddimension method, which can be seen in the comments of the Setmeasureddimension method, which is called in Onmeasure, which is what getmeasuredwidth gets in the view O You cannot assign a value until the size of the view has been obtained in the Nmeasure method. Getmeasuredwidth gets the value measured by onmeasure, which can be called before Onmeasure executes, but gets 0 (the default initialization value of type int).
Above already know the meaning of getmeasuredwidth value, then look at the source code of View.getwidth method:

public class View implements Drawable.callback, Drawable.callback2, Keyevent.callback, Accessibilityeventsource {     /** * Return The width of the your view.     * * @return The width of your view, in pixels. */@ViewDebug. Exportedproperty (category = "layout") public final int getwidth () {//The right side of the view minus the left value retur    n Mright-mleft;     }/** * Assign a size and position to this view.     * * This was called from layout.  * * @param left position, relative to Parent * @param top top position, relative to parent * @param right Right position, relative to parent * @param bottom bottom position, relative to Parent * @return true if the new Size and position are different than the * previous ones * {@hide} */protected Boolean setframe (in        T left, int top, int. right, int bottom) {Boolean changed = FALSE; ...//Any one of the four values changes on the line if (mleft! = left | | Mright! = Right | | mToP! = Top | |            Mbottom! = bottom) {changed = TRUE;            ... mleft = left;            Mtop = top;            Mright = right;            Mbottom = bottom;        ......        }    return changed; } public void layout (int l, int t, int r, int b) {...//Current view layout when executing, passing the upper and lower left and right boundary values of the current view Boolean Chan        Ged = Setframe (L, T, R, b); if (changed | |            (Mprivateflags & layout_required) = = layout_required) {...//the above execution will not trigger OnLayout            OnLayout (changed, L, T, R, b);        ......        }    ... mprivateflags &= ~force_layout; }}

From the above code can be seen when the view layout operation, the first call to the Setframe method passed to left, top, right, bottom these values will be in the future layout analysis is explained in detail, These values are the display position and size (obtained by right-left and Top-bottom) that the parent view sets for his current view. The width of the GetWidth method gets the size that the current view can actually occupy on the screen.
In summary, Getmeasuredwidth is the width specified by the view onmeasure (which can be broadly understood as the size of the content area of the view, although not rigorous but the system provides layout controls like this, only in custom views because the overwrite onmeasure can ignore layout _width,layout_heigh arbitrarily specifies its wide height), and getwidth is the view parent view that specifies the area that the current view can display on the screen.


Android View measure (ii) Customizing UI controls measure related

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.