View measurement of Android GUI

Source: Internet
Author: User

In the previous article (http://blog.csdn.net/jerehedu/article/details/47081849), according to the source code explored the view of the drawing process, the process has three main steps, respectively, measurement, layout, drawing. The system has done a good job of packaging the drawing, we mainly analyze the measurement and layout process and see how Android is measuring and laying out the view.

According to the analysis of the previous article, we know that in the Performmeasure method of Viewrootimpl, we actually call Mview.measure (Childwidthmeasurespec, Childheightmeasurespec ); method. According to the source we found the prototype of the method, this method in the view class, and is the final method, can not be overridden by the quilt class, the specific source of the method is as follows:

Public final void measure (int widthmeasurespec, int. Heightmeasurespec) {Boolean optical = islayoutmodeoptical (this        );        if (Optical! = islayoutmodeoptical (mparent)) {...} Suppress sign extension for the low bytes long key = (long) Widthmeasurespec << 32 |        (long) Heightmeasurespec & 0xffffffffL;        if (Mmeasurecache = = null) Mmeasurecache = new Longsparselongarray (2);                if ((Mprivateflags & pflag_force_layout) = = Pflag_force_layout | |                Widthmeasurespec! = Moldwidthmeasurespec | |                Heightmeasurespec! = Moldheightmeasurespec) {... if (CacheIndex < 0 | | signoremeasurecache) { Measure ourselves, this should set the measured dimension flag back onmeasure (Widthmeasurespe                c, Heightmeasurespec);            MPRIVATEFLAGS3 &= ~pflag3_measure_needed_before_layout; } else {Long value = Mmeasurecache.valueat (CacheinDEX); Casting a long to int drops the high of bits, no mask needed setmeasureddimensionraw ((int) (Value &GT;&G T                (int.), (int) value);            MPRIVATEFLAGS3 |= pflag3_measure_needed_before_layout;        } ... mprivateflags |= pflag_layout_required;        } Moldwidthmeasurespec = Widthmeasurespec;        Moldheightmeasurespec = Heightmeasurespec;                Mmeasurecache.put (Key, ((long) mmeasuredwidth) << 32 | (long) Mmeasuredheight & 0xffffffffL); Suppress sign Extension}

According to the method content and description, you can know that this method is used to measure the size of the view, The two parameters required are built by the parent view to illustrate the specifications of the parent view's measurement of the child view, and actually the method onmeasure the measure, which we will analyze later. Before we first understand the meaning of the two parameters in the measure method, just mentioned that the parameter is the measurement specification of the parent view pair view, then how does Android describe it, here is a class Measurespec, this class is an inner class in view, The key source code is as follows:

 
public static class Measurespec {private static final int mode_shift = 30;        private static final int mode_mask = 0x3 << mode_shift;        public static final int UNSPECIFIED = 0 << mode_shift;        public static final int exactly = 1 << mode_shift;        public static final int at_most = 2 << mode_shift; public static int Makemeasurespec (int size, int mode) {if (Susebrokenmakemeasurespec) {return            Size + mode; } else {return (size & ~mode_mask) |            (Mode & Mode_mask);        }} public static int getmode (int measurespec) {return (Measurespec & Mode_mask);        } public static int getsize (int measurespec) {return (Measurespec & ~mode_mask); } ...} 

According to the SDK, this class encapsulates the layout requirements for parent View sub-view, each representing the height or width of a pair of view requirements, and the measurement requirements are two sections, respectively, dimensions and patterns. The model consists of three main types, as follows:

1. UNSPECIFIED: There is no constraint on behalf of the parent view pair view, child view can be any size.

2, exactly: The parent view determines the size of the child view, the sub-view is limited to the given boundary, and the size of its own.

3, At_most: Child view maximum can reach a specified size value.

The method used to calculate and generate the measurement requirements is provided in this class, as follows:

1, public static int makemeasurespec (int size, int mode), this method eventually generates a 32-bit binary number to indicate the measurement specification requirements, where 32 and 31 bits are used to indicate the pattern, and the latter 30 bits represent the size.

2, public static int getmode (int measurespec), this method can be calculated according to the measurement description, the mode.

3, public static int getsize (int measurespec), this method calculates the size according to the measurement description.

Understand Measurespec, we are looking back, take a look at the Onmeasure method, the source of the method is as follows:

    protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {        setmeasureddimension (getdefaultsize ( Getsuggestedminimumwidth (), Widthmeasurespec),                getdefaultsize (Getsuggestedminimumheight (), HeightMeasureSpec ));}

The default implementation of this method is very simple, and the Setmeasureddimersion method is called to save the measured dimensions to Mmeasuredwidth and mmeasuredheight. And in the Setmeasureddimersion method called the getdefaultsize used to calculate, the method is as follows:

public static int getdefaultsize (int size, int measurespec) {        int result = size;        int specmode = Measurespec.getmode (measurespec);        int specsize = measurespec.getsize (measurespec);        Switch (specmode) {case        measurespec.unspecified:            result = size;            break;        Case Measurespec.at_most: Case        measurespec.exactly:            result = specsize;            break;        }        return result;    }

Obviously, this method calculates the actual size of the view based on the default size and measurement requirements provided. This concludes the measurement process. In most cases, however, when we customize the ViewGroup, we need to rewrite the Onmeasure method, in which we can iterate through all the sub-view and ask them to measure their size, Also do not forget to call Setmeasureddimension to save the measurement results, the ViewGroup is implemented by the following three methods, the key code is as follows:

Method Mesurechildren, iterates through all non-hidden sub-view and calls the Measurechild method to set the measurement requirements of the child view.

protected void Measurechildren (int widthmeasurespec, int heightmeasurespec) {        final int size = Mchildrencount;        Final view[] children = Mchildren;        for (int i = 0; i < size; ++i) {            final View child = Children[i];            if ((Child.mviewflags & visibility_mask)! = GONE) {                measurechild (child, Widthmeasurespec, Heightmeasurespec); c8/>}}}        

Method Measurechild, gets the measurement specification of the child view, and calls measure to measure the actual size.

protected void Measurechild (View child, int parentwidthmeasurespec,            int parentheightmeasurespec) {        final Layoutparams LP = Child.getlayoutparams ();        Final int childwidthmeasurespec = Getchildmeasurespec (Parentwidthmeasurespec,                mpaddingleft + mpaddingright, Lp.width);        Final int childheightmeasurespec = Getchildmeasurespec (Parentheightmeasurespec,                mpaddingtop + MPaddingBottom, Lp.height);        Child.measure (Childwidthmeasurespec, childheightmeasurespec);}

Method Getchildmeasurespec is used to obtain the measurement specification requirements for the view.

    public static int Getchildmeasurespec (int spec, int padding, int childdimension) {int specmode = MEASURESPEC.G        Etmode (spec);        int specsize = measurespec.getsize (spec);        int size = Math.max (0, specsize-padding);        int resultsize = 0;        int resultmode = 0; Switch (Specmode) {///Parent has imposed a exact size on US case MeasureSpec.EXACTLY:if (child                Dimension >= 0) {resultsize = childdimension;            Resultmode = measurespec.exactly; } else if (childdimension = = layoutparams.match_parent) {//child wants to is our size.                So is it.                resultsize = size;            Resultmode = measurespec.exactly; } else if (childdimension = = layoutparams.wrap_content) {//child wants to determine its own size.                It can ' t be//bigger than us.                resultsize = size;     Resultmode = Measurespec.at_most;       } break;                Parent have imposed a maximum size on US case MeasureSpec.AT_MOST:if (childdimension >= 0) {                Child wants a specific size ... so is it resultsize = childdimension;            Resultmode = measurespec.exactly; } else if (childdimension = = layoutparams.match_parent) {//child wants to being our size, but we size is no                T fixed.                Constrain bigger than us.                resultsize = size;            Resultmode = Measurespec.at_most; } else if (childdimension = = layoutparams.wrap_content) {//child wants to determine its own size.                It can ' t be//bigger than us.                resultsize = size;            Resultmode = Measurespec.at_most;        } break; Parent asked to see how big we want to is case MeasureSpec.UNSPECIFIED:if (childdimension >= 0) {//child wants a specific size ... let him has it resultsize = childdimension;            Resultmode = measurespec.exactly;  } else if (childdimension = = layoutparams.match_parent) {//child wants to is our size ... find out how big                It should/be resultsize = 0;            Resultmode = measurespec.unspecified;  } else if (childdimension = = layoutparams.wrap_content) {//child wants to determine its own size .... find                How about//big it should be resultsize = 0;            Resultmode = measurespec.unspecified;        } break;    } return Measurespec.makemeasurespec (ResultSize, Resultmode); }

Inquiries or technical exchanges, please join the official QQ Group: (452379712)

Jerry Education
Source:http://blog.csdn.net/jerehedu/
This article is the copyright of Yantai Jerry Education Technology Co., Ltd. and CSDN Common, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

View measurement of Android GUI

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.