An analysis of the drawing process of Android UI (c) Layout process

Source: Internet
Author: User

Objective

In the previous blog, I learned that the measure process measured the view and got measuredwidth/measuredheight. For ViewGroup, the width of all children is calculated to sum. This article to analyze the layout process.

Layout overview

The layout method assigns size and position to a view and its descendants, which is the second step in the view drawing process (the second phase of layout mechanism), which uses the width of the previous step measure. Similar to Measure-onmeasure, the OnLayout method is also recalled in layout. But a bit different, layout is not declared final.

Child view should not rewrite onlayout after inheritance, and when the child view is a viewgroup, you need to rewrite onlayout to call layout for each view below the ViewGroup.

Layout four parameters L, T, R, b indicate the distance relative to Parentview, the most common one is (0, 0, width, height).

     Public voidLayoutintLintTintRintb) {if((MPRIVATEFLAGS3 & pflag3_measure_needed_before_layout)! = 0) {onmeasure (Moldwidthmeasurespec, Moldheightmeasurespec); MPRIVATEFLAGS3&= ~pflag3_measure_needed_before_layout; }        intOLDL =Mleft; intOldt =Mtop; intOldb =Mbottom; intOldr =Mright; BooleanChanged = Islayoutmodeoptical (mparent)?setopticalframe (L, T, R, B): Setframe (L, T, R, b); if(Changed | | (Mprivateflags & pflag_layout_required) = =pflag_layout_required)            {onlayout (changed, L, T, R, b); Mprivateflags&= ~pflag_layout_required; Listenerinfo Li=Mlistenerinfo; if(Li! =NULL&& Li.monlayoutchangelisteners! =NULL) {ArrayList<OnLayoutChangeListener> listenerscopy =(ArrayList<OnLayoutChangeListener>) Li.mOnLayoutChangeListeners.clone (); intNumlisteners =listenerscopy.size ();  for(inti = 0; i < numlisteners; ++i) {listenerscopy.get (i). Onlayoutchange ( This, L, T, R, B, Oldl, Oldt, Oldr, OLDB); } }} mprivateflags&= ~pflag_force_layout; MPRIVATEFLAGS3|=pflag3_is_laid_out; }

Layout method, first of all according to the flag bit to determine whether the need to re-measure, and then to determine whether to optical, this parameter to control the projection and other effects, usually directly into the setframe,setframe to determine whether the view size has changed, To determine if it is necessary to redraw.

Then the callback onlayout, in view, OnLayout is an empty shell, nothing to do, in the ViewGroup, then the child view call layout.

If layout changes, the layout values before and after all registered listener changes are notified.

Setframe

Returns true when size and position change. If a change occurs, invalidate is called inside the Setframe method.

OnLayout

View OnLayout do nothing, in ViewGroup, according to their actual rules (Linear, Relative, etc.) to the internal views of the layout arrangements.

Getmeasuredwidth and GetWidth
    • The timing can be called differently: Getmeasuredwidth can be called after measure, getwidth to be called after layout. (returns 0 if called before the opportunity occurs)
    • The meaning is different: Getmeasuredwidth is the view calculates its actual size, getwidth is the size after the layout. Simplest of all, in Scrolllayout, GetHeight returns the height inside the screen, Getmeasuredheight returns the total height of the screen + offscreen.
Summary

This is simple, because in most cases, there is no need to rewrite the OnLayout method; Once you rewrite the method (ViewGroup), you will find that there are many things to do, see Linearlayout.onlayout, Relativelayout.onlayout.

The next article begins OnDraw ().

An analysis of the drawing process of Android UI (c) Layout process

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.