View的draw流程2

3.绘制View的内容

这一步调用了View的onDraw方法。这个方法是一个空实现,因为不同的view有着不同的内容。所以这需要我们自己去实现。就是在自定义View中重写该方法来实现。

4.绘制子view

这一步调用了dispatchDraw方法。这个方法也是一个空实现。ViewGroup中重写了这个方法。在ViewGroup的dispatchDraw方法中,对子View进行遍历。并调用drawChild方法。在drawChild方法中,主要调用了view的draw方法。在draw方法中,会判断有没有缓存,如果没有,则正常绘制。如果有,则使用缓存显示。

6.绘制装饰

这一步是使用View的onDrawForeground方法。

 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);
        }
    }

很明显这个方法用于绘制scrollBar以及其他装饰。并将它们绘制在视图内容的上层。

猜你喜欢

转载自blog.csdn.net/howlaa/article/details/128717353