Android View Framework Summary (vi) Draw process for view layout process

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/hejjunlin/article/details/52236145

    • Draw timing diagram for view
    • Viewrootimpl.performtraversals process
    • Viewrootimpl.performdraw process
    • View.draw method
    • View.dispatchdraw process
    • The OnDraw process of linearlayout
Draw timing diagram for view

Previous several through the view tree measure and layout process analysis of things, Next will be combined with the first two steps to get the measured values and in the view of the bit position, began to draw operations, one step more complex, draw process than the previous complex, draw bad, will appear overdraw. Below, please look at the analysis process carefully:
The original trigger point of draw is also the start of the analysis process in the Viewrootimpl performtraversals (Execute traversal) method: It can be combined with the timing diagram I've drawn to make it easier to understand.

Viewrootimpl.performtraversals ()

Viewrootimpl.performdraw ()

Viewrootimpl.draw ()

The measure and layout processes directly invoke the Viewrootimpl Performmeasure and PerformLayout methods, and draw calls Viewrootimpl's Performdraw () method, The Dispatchondraw () method in Viewtreeobserver is then called by the Draw (Boolean fullredrawneeded) method in Performdraw, Make a notification all the view that hangs on the view tree starts draw.

Viewtreeobserver.dispatchondraw ()

View.ondraw ()

For View.java and Viewgroup.java,ondraw () the default is NULL implementation, because the specific view itself is what, this is to do the framework, provide space, you want to customize what view inside is determined by the user, but can provide the default method.

View.draw ()


Another draw method for view

View.drawbackground ()-//Step 1, draw the background

View.ondraw ()-//Step 3, draw the content

View.dispatchdraw ()-//Step 4, draw the children

view.ondrawscrollbars-//Step 6, draw decorations (foreground, scrollbars)

Dispatchdraw () In view defaults to an empty implementation because it does not contain child view, and ViewGroup overrides Dispatchdraw () to draw on its child view, general custom view should not overload Dispatchdraw (). Because it already embodies the process of drawing the view system. So, let's continue to analyze the specific process of Dispatchdraw () in ViewGroup:

Viewgroup.dispatchdraw ()

Viewgroup.drawchild ()

    • The key to Dispatchdraw () is to draw each child view of ViewGroup with a For Loop call Drawchild (), if Flag_use_child_drawing_order is true in the preceding code, The drawing order of the child views is determined by Getchilddrawingorder, the default drawing order is the order in which the child views join the ViewGroup, and we can overload the Getchilddrawingorder method to change the default drawing order. Let the child view overlap on the parent view, or hang it on the parent view.
    • The core of Drawchild () is to assign the appropriate canvas canvas area to the child view, and the size of the canvas area is determined by D in the layout process, and the position of the canvas area depends on the scrolling value and the current animation of the child view. When the canvas area is set, the draw () function of Childview is called, and if Childview contains the Skip_draw identity, only Dispatchdraw () is called, that is, the drawing of the child view itself is skipped, but the childview that the view may contain is drawn.
Viewgroup.drawchild ()

Draw Process Small Summary:

    • A custom view is a viewgroup, you need to draw all of the contained child view recursively.
    • View default does not draw any content, the real draw all need to implement in the subclass itself, just do the drawing process, this is the framework responsibility.
    • Distinguish between the view animation and the ViewGroup layout animation, which refers to the view itself animation, can be added through Setanimation, which is specifically for the ViewGroup display internal sub-view when the animation set. You can set the Layoutanimation property for ViewGroup in the XML layout file (for example, when you set a child view to LinearLayout to show a line-by-row, random, inferior display, and so on).
    • The padding is automatically processed when getting the canvas clipping area (the canvas passed in in draw for each view), and the child view gets canvas without paying attention to these logic, only with care about how to draw.
    • By default, the Viewgroup.drawchild drawing order and child view are added in the same order, but you can also overload the Viewgroup.getchilddrawingorder () method to provide a different order.

The first time to get blog update reminders, as well as more Android dry, source code Analysis , Welcome to follow my public number, sweep the bottom QR code or long press to identify two-dimensional code, you can pay attention to.

If you feel good, easy to praise, but also to the author's affirmation, can also share this public number to you more people, original not easy

Android View Framework Summary (vi) Draw process for view 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.