Android View drawing process

Drawing process of View
Take DecorView for example
The draw process is still started from the performTraversals of ViewRootImpl. After a series of calls, the draw method of View is called. Since ViewGroup does not override the draw method, all views call the View draw method. Therefore, we can directly see its source code:

    public void draw(Canvas canvas) {
        final int privateFlags = mPrivateFlags;
        final boolean dirtyOpaque = (privateFlags & PFLAG_DIRTY_MASK) == PFLAG_DIRTY_OPAQUE &&
                (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
        mPrivateFlags = (privateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN;

        /*
         * Draw traversal performs several drawing steps which must be executed
         * in the appropriate order:
         *
         *      1. Draw the background
         *      2. If necessary, save the canvas' layers to prepare for fading
         *      3. Draw view's content
         *      4. Draw children
         *      5. If necessary, draw the fading edges and restore layers
         *      6. Draw decorations (scrollbars for instance)
         */

        // Step 1, draw the background, if needed
        int saveCount;

        if (!dirtyOpaque) {
            drawBackground(canvas);
        }

        // skip step 2 & 5 if possible (common case)
        final int viewFlags = mViewFlags;
        boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
        boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
        if (!verticalEdges && !horizontalEdges) {
            // Step 3, draw the content
            if (!dirtyOpaque) onDraw(canvas);

            // Step 4, draw the children
            dispatchDraw(canvas);

            // Overlay is part of the content and draws beneath Foreground
            if (mOverlay != null && !mOverlay.isEmpty()) {
                mOverlay.getOverlayView().dispatchDraw(canvas);
            }

            // Step 6, draw decorations (foreground, scrollbars)
            onDrawForeground(canvas);

            // we're done...
            return;
        }
        . . . Omit...
    }

The drawing process of View has the following six steps:
1. Draw the background of View: View.drawBackground
2. Save current layer information (skip)
3. Draw the content of View: View.OnDraw or overridden OnDraw
4. To draw a child View of a View (if any): dispatchDraw traverses and calls the draw method of all child elements.
5. Draws the faded edge of the View, similar to the shadow effect (can be skipped)
6. Draw the decoration of View: View.ondrawforegroup

Posted by Brad420 on Sat, 04 Apr 2020 04:01:18 -0700