Realization of the implementation -2-onmeasure of flow-type layout

Source: Internet
Author: User

In this article, we are mainly to implement the Onmeasure function in FlowLayout.

First of all, say onmeasure, you can say Overload onmeasure (), OnLayout (), OnDraw () three functions to build a custom view of the appearance of the image. Coupled with the behavior of overloaded views such as ontouchevent (), you can build any perceived custom view that we need. We know that both the custom view and the system-provided textview have to be placed in some viewgroup such as linearlayout, so theoretically we can understand onmeasure (), OnLayout (), OnDraw () These three functions: 1. The size of the view itself, which is determined by onmeasure (); 2. The location of the view in ViewGroup, which is determined by OnLayout (); 3. Drawing View,ondraw () defines how to draw the view.

The onmeasure in TextView are:

protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {         int widthmode = Measurespec.getmode ( WIDTHMEASURESPEC);         int heightmode = Measurespec.getmode (heightmeasurespec);         int widthsize = measurespec.getsize (widthmeasurespec);         int heightsize = measurespec.getsize (heightmeasurespec);           int width;         int height;           ...          if (Widthmode = = measurespec.exactly) {             //Parent has told us how big to be. So is it.             width = widthsize;                   }          else {           if (mlayout! = null && mellipsize = = null) {                       des = desired (mlayout);                         }                        ...             }                Setmeasureddimension (width, height);      }

The first thing we need to understand is Widthmeasurespec, where did the Heightmeasurespec come from? The Onmeasure () function is called by the specific viewgroup that contains the view, so the value is also passed in from this viewgroup. Here I give the answer directly: the two parameters of the subclass view are determined by the layout_width,layout_height and padding in ViewGroup and the layout_margin of the view itself. Weight weight is also a particular factor to consider, and its existence can be slightly more complicated.

Understand the source of these two parameters, and also know the effect of these two values. We only take Heightmeasurespec as a description. This value consists of a high of 32 bits and a low of 16 bits , and a high 32-bit saved value is called Specmode, which can be obtained by Measurespec.getmode () as shown in the code; The low 16 bits are specsizeand can also be obtained by Measurespec.getsize (). So what is the role of Specmode and specsize? To know this, we need to know the last line in the code, and the last line of all the view's Onmeasure () calls the function of the setmeasuredimension () function-the value passed in the function call is the view's final size. In other words, all the work done in Onmeasure () is for the last sentence.

Then we started a piece of analysis code:

The first part:
int sizewidth = measurespec.getsize (widthmeasurespec); int modewidth = Measurespec.getmode (widthmeasurespec); int Sizeheight = Measurespec.getsize (heightmeasurespec); int modeheight = Measurespec.getmode (HeightMeasureSpec);

Get the size and mode in Widthmeasurespec,heightmeasurespec, respectively:

Getsize,getmode and the variables involved in the process:

public static int GetMode (int measurespec) {      return (Measurespec & Mode_mask);} public static int GetSize (int measurespec) {       return (Measurespec & ~mode_mask);} private static Final int mode_sh  IFT = 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 << M Ode_shift;
Part II:
        int width = 0;        int height = 0;         Record the width and height of each line        int linewidth = 0;        int lineheight = 0;         Get the number of internal elements        int ccount = Getchildcount ();
It is explained in the comments, it does not explain;
Part III:
  for (int i = 0; i < ccount; i++) {View child = Getchildat (i);            Measure sub-view width and height measurechild (child, Widthmeasurespec, Heightmeasurespec);             Get layoutparams marginlayoutparams LP = (marginlayoutparams) child. Getlayoutparams (); Child view occupies a width of int childwidth = Child.getmeasuredwidth () + Lp.leftmargin + lp.right            Margin;             Child view occupies a height of int childheight = Child.getmeasuredheight () + Lp.topmargin + lp.bottommargin;            NewLine if (linewidth + childwidth > Sizewidth-getpaddingleft ()-getpaddingright ())                {//Compare to get maximum width = Math.max (width, linewidth);                Reset LineWidth linewidth = childwidth;                Record line High height + = lineheight;            Lineheight = Childheight;   } else//No line break {             Overlay row width linewidth + = childwidth;            Get the maximum height of the current line lineheight = Math.max (lineheight, childheight);                }//Last control if (i = = cCount-1) {width = Math.max (linewidth, width);            Height + = lineheight; }        }

First get the child view, and then get sub-view layoutparams, because there is spacing, so pay attention to sub-view of the layoutparams forced conversion to marginlayoutparams;

Recalculation of the width of the sub-view: Because there is left and right spacing, so the new width is the original width plus left-and-right spacing;

After you decide not to wrap, that is, the calculated line width plus the new sub-view width, if the width of greater than flowlayout minus the left and right margin description of the remaining widths, not enough to add a new sub-view, to put the new sub-view into a new row (in fact, have done ACM, you can use Line tree to be optimized), otherwise added to the original line. Then in both cases, the line height, line width, height and width do different adjustments;

If the last sub-view: If you want to change the line, high is only recorded to the previous section, and the new height after the line is the last sub-view of the high no Plus, wide words, if the last sub-view of the width of a large more than before, there is no update; The height and width of the last line is always not added; So the last view to be a special award, because whether it is a newline or no line, line width has been updated, so long as the maximum width to do the comparison, high as long as the line is high;

Part IV:
  Setmeasureddimension (                //                Modewidth = = measurespec.exactly? Sizewidth:width + getpaddingleft () + Getpaddingright (),                modeheight = = measurespec.exactly? Sizeheight:height + getpaddingtop () + getpaddingbottom ()//< c7/>);
Judge whether the measurespec.exactly is not the content is covered with the parent container, is the case, directly will be the first call GetSize get the width of the high incoming can, if not, the measured width and height plus margin into the setmeasureddimension function, to determine the view of the final visual Figure size.


Realization of the implementation -2-onmeasure of flow-type layout

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.