Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of View drawing process in Android?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the principle of View drawing process in Android? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

SetContentView process

The whole process of setContentView is mainly how to add the layout file of Activity or the View of java to the window, which can be summarized as follows:

Create a DecorView object, mDecor, that will serve as the root view of the entire application window.

Create different window decoration layout files according to style theme such as Feature, and obtain the place where the Activity layout file should be stored through findViewById (window decoration layout file in the FrameLayout where id is content).

Add the layout file of Activity to the FrameLayout whose id is content.

When setContentView is set to display OK, the onContentChanged method of Activity is called back. Various View findViewById () methods of Activity can be put into this method, and the system will help with the callback.

View drawing of android

View drawing mainly includes three aspects:

Measure measures the size of the component itself

Layout determines the location of the component in the view

Draw draws the components according to the location and size

The starting point of view drawing is the performTraversals () method of the ViewRootImpl class. The main work of this method is to determine whether to recalculate the test view size (measure), the Buddha reposition view position (layout) and whether to redraw the view (draw) according to the previous state. Part of the source code is as follows:

Private void performTraversals () {. / / the origin of the widthMeasureSpec and heightMeasureSpec of the outermost root view / / lp.width and lp.height equals MATCH_PARENT int childWidthMeasureSpec = getRootMeasureSpec (mWidth, lp.width) when creating the ViewGroup instance; int childHeightMeasureSpec = getRootMeasureSpec (mHeight, lp.height);. MView.measure (childWidthMeasureSpec, childHeightMeasureSpec);. MView.layout (0,0, mView.getMeasuredWidth (), mView.getMeasuredHeight ()); MView.draw (canvas); } measure calculates the view size

Almost all components inherit the View class, and the daily development methods for view measurement are measure and onMeasure. Measure cannot be rewritten. When we customize it, we can mainly rewrite the onMeasure method. Inside the method, we must measure the actual size of the mMeasuredWidth and mMeasuredHeight of the component, which needs to be determined by both the parent view and the child view.

The measure process traverses the entire view tree structure from the root view measure, as follows:

Write the picture description here.

Also note that the view size MeasureSpec is a combined size, which is a 32-bit value, the high two bits are the size mode specMode, and the lower 30 bits are the size values. We can use the original sound library method provided to easily combine and disassemble the dimensions:

There are three types of specMode: MeasureSpec.EXACTLY for determined size, MeasureSpec.AT_MOST for maximum size, and MeasureSpec.UNSPECIFIED for uncertainty

Int measureSpec = MeasureSpec.makeMeasureSpec (windowSize, MeasureSpec.EXACTLY); / / synthetic int specMode = MeasureSpec.getMode (measureSpec); / / dismantled int specSize = MeasureSpec.getSize (measureSpec)

In the view measurement meause, the parent component usually transfers a combined size to the child component. We can take out the specific size and generate a new dimension value according to other conditions, and use setMeasuredDimension to set the specific size of mMeasuredWidth and mMeasuredHeight to complete the measurement.

Summary of measure principle

The MeasureSpec (internal class of View) measurement specification is int, and the value consists of a high 2-bit specification mode specMode and a low 30-bit specific size specSize. Where specMode has only three values:

MeasureSpec.EXACTLY / / determine the mode. The parent View wants the size of the child View to be determined, which is determined by the specSize. For the most modes, the parent View expects the size of the child View to be the value specified by specSize. MeasureSpec.UNSPECIFIED / / No mode is specified, and the parent View is determined entirely according to the design value of the child View.

The measure method of View is final, and overloading is not allowed, and the view subclass can only overload onMeasure to complete its own measurement logic.

The MeasureSpec of the topmost DecorView measurement is determined by the getRootMeasureSpec method in ViewRootImpl (the width and height parameters of LayoutParams are all MATCH_PARENT,specMode and EXACTLY,specSize is the physical screen size).

The ViewGroup class provides measureChild,measureChild and measureChildWithMargins methods that simplify the size calculation of parent-child View.

As long as it is a subclass of ViewGroup, you must ask LayoutParams to inherit the child MarginLayoutParams, otherwise you cannot use the layout_margin parameter.

The layout size of the View is determined by both the parent View and the child View.

Using the getMeasuredWidth () and getMeasuredHeight () methods of View to get the width and height of the View measurement, you must ensure that these two methods are called after the onMeasure process to return valid values.

Layout view location determination

The main process of layout is to traverse the whole view tree structure and call view.layout (int l, int t, int r, int b) to determine the specific coordinates of view. The flow chart is as follows.

Write the picture description here.

When we customize a component, we usually rewrite the onLayout method to implement our own logic. Finally, we call the layout method to determine the location of the view. If we customize the component with a ViewGroup, we also need to traverse each child to determine the size.

Summary of layout principle

The whole layout process is easy to understand. From the above analysis, we can see that layout is also the process of recursively calling the view.layout method from the top-level parent View to the child View, that is, the parent View places the child View in the appropriate location according to the layout size and layout parameters obtained by the feature child View in the previous step. The specific layout core mainly includes the following points:

The View.layout method can be overloaded, ViewGroup.layout is not overloaded for final, ViewGroup.onLayout is abstract, and subclasses must overload to implement their own location logic.

After the completion of the measure operation, the measured measuredWidth and measuredHeight,layout operations for each View are the mLeft, mTop, mRight, mBottom after the location allocation to each View, these values are relative to the parent View.

All layout_XXX layout attributes are basically aimed at ViewGroup with child View, when setting the relevant layout_XXX attribute to a View without a parent container is meaningless ("Android application setContentView and LayoutInflater load parsing mechanism source code analysis" was also mentioned earlier).

Using the getWidth () and getHeight () methods of View to get the width and height of the View measurement, you must ensure that these two methods are called after the onLayout process to return valid values.

Draw drawing

After completing the measure and Layout, the code in ViewRootImpl creates a Canvas object, and then calls the draw () method of View to perform the specific painter. So we return to the tree recursive draw process of ViewGroup and View.

First, let's take a look at the recursive draw flow chart of the View tree, as follows:

Write the picture description here.

Summary of draw principle

As you can see, the drawing process is to draw the View object to the screen. The whole draw process needs to pay attention to the following details:

If the View is a ViewGroup, you need to recursively draw all the child View it contains.

View does not draw anything by default, and the real drawing needs to be implemented in subclasses.

View is drawn with the help of the Canvas class passed in by the onDraw method.

Distinguish between View animation and ViewGroup layout animation. The former refers to the animation of View itself, which can be added through setAnimation, while the latter is animated specifically for ViewGroup when displaying internal sub-views. You can set layoutAnimation properties for ViewGroup in the xml layout file (for example, setting sub-View for LinearLayout to appear line-by-line, random, lower, and other different animation effects during display).

The padding is automatically disposed of when getting the canvas clipping area (the Canvas passed in the draw of each View). The sub-View gets the Canvas without paying attention to the logic, just care about how to draw it.

By default, the ViewGroup.drawChild drawing order of the child View is the same as the order in which the child View is added, but you can also overload the ViewGroup.getChildDrawingOrder () method to provide a different order.

Source code analysis of invalidate and postInvalidate methods for API control view provided by view

Request to redraw the view, call draw

Invalidate is called in the main thread

PostInvalidate is called on a non-main thread

RequestLayout method of View

The requestLayout () method calls the measure procedure and the layout procedure, does not call the draw procedure, and does not redraw any View, including the caller itself.

After reading the above, have you mastered the principle of View drawing process in Android? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report