Android Interview Collection record view measurement, layout and drawing principles

Source: Internet
Author: User

First, the view drawing process framework

The drawing of the view is iterated from the top down. Decorview-->viewgroup (--->viewgroup)-->view, follow this process from top to bottom, measure (measurement), layout, Draw (draw).

Second, measure process

As the name implies, the size of each control is measured.

Call the measure () method, do some logical processing, and then call the Onmeasure () method, where you call Setmeasureddimension () to set the view's width height information to complete the view's measurement operation.

void measure (int heightmeasurespec) {}

In the measure () method, two parameters Widthmeasurespec are passed in, and Heightmeasurespec represents some information about the width and height of the view.

void Onmeasure (int heightmeasurespec) {setmeasureddimension (Getdefaultsize (Getsuggestedminimumwidth (), WIDTHMEASURESPEC), Getdefaultsize (Getsuggestedminimumheight (), heightmeasurespec)); }

From the above process, the measure process is very simple, the key point is Widthmeasurespec, heightmeasurespec these two parameter information how to obtain?

If there is a widthmeasurespec, heightmeasurespec, through certain processing (can be overridden, custom processing steps), from which to get the view of the width/height, call the Setmeasureddimension () method, specify the view's width height To complete the measurement work.

The determination of Measurespec

What is measurespec first?

The Measurespec is made up of two parts, one measuring mode and the other part measuring size.

Among them, mode pattern is divided into three categories

UNSPECIFIED: Do not limit the view, how much to how big, generally used in the system internal

Exactly: corresponds to the layoutparams in the match_parent and the specific values of the two modes. The exact size required for the view is detected, when the final size of the view is the value specified by Specsize,

At_most: Corresponds to Wrap_content in Layoutparams. The size of the view cannot be greater than the size of the parent container.

So how is Measurespec determined?

For Decorview, it is determined by the size of the screen, and by its own layout parameters layoutparams.

This section is very simple, depending on the layout format of the Layoutparams (match_parent,wrap_content or the specified size), the size of itself, compared to the screen size, set a width of not exceeding the screen size, and corresponding mode.

For other view (including ViewGroup), it is determined to be layoutparams by the parent layout's measurespec and its own layout parameters.

This part is more complicated. The following chart shows the different situations:

When the Layoutparams layout format of the Sub view is Wrap_content, you can see that the size of the child view is the remaining size of the parent view, and the size of the child view is not different when set to Match_parent. In order to show the difference, in general, when customizing view, you need to override the Onmeasure method, handle wrap_content, and make a special designation.

From here, Measurespec's designation is also a layer down from the top-level layout, and the parent layout affects the sub-layout.

Maybe about measurespec how to determine the size of the view is also somewhat vague, limited space, no detailed introduction, you can read this article

The measurement flow of the view:

Third, layout process

After you have measured the view size, you need to place the view in the window, and the layout of the view is determined primarily by determining the top or bottom four points.

The layout is also top-down, the difference is viewgroup first in layout () to determine their own layout, and then in the OnLayout () method and then call the Child View layout () method, let the child view layouts. In the measure process, viewgroup generally measures the size of the child view before determining its size.

Publicvoid Layout (int L,int T,int R,int b) {Four vertices of the current viewint OLDL= Mleft;int Oldt= Mtop;int Oldb= mbottom; int oldr = mright; //setframe ()/Setopticalframe (): Determine the location of the view itself //initializes the values of the four vertices and then determines whether the current view size and position have changed and returns boolean changed = Islayoutmodeoptical (mparent)  Setopticalframe (L, T, R, b) : Setframe (L, T, R, b); //if the size and position of the view changes, OnLayout () if (changed | | (Mprivateflags & pflag_layout_required) == pflag_layout_required) {//onlayout (): Determines the location of all child view of the view in the parent container OnLayout (changed, L, T, R, b);  ...}              

It can be seen through Setframe ()/Setopticalframe (): Determines the location of the view itself, and determines the layout of the Child view by OnLayout (). Setopticalframe () is also called the Setframe (), so specifically see Setframe () How to determine their position layout.

Boolean setframe (... //The position information of the view is recorded by the following assignment statement, which determines the four vertices = bottom; Mrendernode. Setlefttoprightbottom (Mleft, Mtop, Mright, Mbottom);}   

Once you have identified your position, you must determine the layout of the child view through OnLayout (). OnLayout () is an inheritable, empty method.

void OnLayout (int bottom) {}

If the current view is a single view, then there is no child view and you do not need to implement the method.

If the current view is a viewgroup, you need to implement the OnLayout method, which is related to its characteristics when implementing a custom viewgroup, and must be implemented by itself.

This completes the layout of a layer of work.

Layout process for view:

Four, draw process

The drawing process for view follows these steps:

① Drawing background background.draw (canvas)

② Draw Yourself (OnDraw)

③ Drawing Children (Dispatchdraw)

④ drawing Decoration (ondrawscrollbars)

The order of drawing can be clearly seen from the source code.

Publicvoid Draw (Canvas canvas) {All views are ultimately called draw () drawing views of view (ViewGroup does not replicate this method)When customizing a view, the method should not be replicated, but instead the OnDraw (Canvas) method is used for drawing.If the custom view is really going to replicate the method, then you need to call Super.draw (canvas) to finish drawing the system before customizing the drawing....int savecount;if (!dirtyopaque) {Step 1: Draw your own view background drawbackground (canvas); }Save the layer if necessary (there is also a resilient layer)Optimization tips://when you don't need to draw a layer, the two steps of Save layer and undo layer skip //so you save layer when you draw can improve drawing efficiency final int viewflags = mviewflags; if (!verticaledges && ! Horizontaledges) {if (!dirtyopaque) / /Step 2: Draw itself view content default to the empty implementation, custom view needs to be replicated OnDraw (canvas);  ... //Step 3: Draw sub View default to NULL implementation of a single view does not need implementation, ViewGroup has implemented the method Dispatchdraw (canvas); Span class= "PL-C1" > ..... //Step 4: Draw sliders and foreground colors, etc. ondrawscrollbars (canvas); return;}  ...}              

This process needs to be implemented for both ViewGroup and single view, unlike the Dispatchdraw () method implemented in ViewGroup, which is not required in a single child view. Custom view generally overrides the OnDraw () method, in which different styles are drawn.

View Drawing process:

V. Summary

From the view measurement, layout and drawing principle, to implement a custom view, depending on the kind of custom view, you might want to customize the implementation of different methods. But these methods are all: the Onmeasure () method, the OnLayout () method, the OnDraw () method.

Onmeasure () Method: Single view, generally override this method, for the Wrap_content case, specify the view default size value, to avoid the match_parent situation consistent. ViewGroup, if not rewritten, executes the same logic as the list view and does not measure sub-view. The Onmeasure () method is generally overridden to iterate through the sub-view.

**onlayout () Method: * * Single view, do not need to implement this method. ViewGroup must be implemented, and the method is an abstract method that implements this method to lay out a pair of view.

**ondraw () Method: * * Regardless of the single view, or ViewGroup need to implement the method, because it is an empty method

Vi. Articles of Reference

Https://github.com/LRH1993/android_interview/blob/master/android/basis/custom_view.md

Android Interview Collection record view measurement, layout and drawing principles

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.