View's draw process 2

3. Draw the content of the View

This step calls the View's onDraw method. This method is an empty implementation, because different views have different content. So this needs to be implemented by ourselves. It is achieved by overriding this method in the custom View.

4. Draw subviews

This step calls the dispatchDraw method. This method is also an empty implementation. This method is overridden in ViewGroup. In the dispatchDraw method of ViewGroup, traverse the child View. And call the drawChild method. In the drawChild method, the draw method of the view is mainly called. In the draw method, it will be judged whether there is a cache, and if not, it will be drawn normally. If available, display using cache.

6. Draw decorations

This step is to use View's onDrawForeground method.

 public void onDrawForeground(Canvas canvas) {
        onDrawScrollIndicators(canvas);
        onDrawScrollBars(canvas);

        final Drawable foreground = mForegroundInfo != null ? mForegroundInfo.mDrawable : null;
        if (foreground != null) {
            if (mForegroundInfo.mBoundsChanged) {
                mForegroundInfo.mBoundsChanged = false;
                final Rect selfBounds = mForegroundInfo.mSelfBounds;
                final Rect overlayBounds = mForegroundInfo.mOverlayBounds;

                if (mForegroundInfo.mInsidePadding) {
                    selfBounds.set(0, 0, getWidth(), getHeight());
                } else {
                    selfBounds.set(getPaddingLeft(), getPaddingTop(),
                            getWidth() - getPaddingRight(), getHeight() - getPaddingBottom());
                }

                final int ld = getLayoutDirection();
                Gravity.apply(mForegroundInfo.mGravity, foreground.getIntrinsicWidth(),
                        foreground.getIntrinsicHeight(), selfBounds, overlayBounds, ld);
                foreground.setBounds(overlayBounds);
            }

            foreground.draw(canvas);
        }
    }

Obviously this method is used to draw scrollBar and other decorations. and draw them on top of the view content.

Guess you like

Origin blog.csdn.net/howlaa/article/details/128717353