Android中View绘制过程(六) performDraw

来源:互联网 发布:java class.this 编辑:程序博客网 时间:2024/05/22 10:59

performTraversals()中执行了performmeasure确定了每一个View的width,和height, 就会调用performLayout,确定这些View在屏幕

的位置,调用performDraw()在每一个View将图像画在屏幕上自己layout确定的位置上就可以了。

调用如下performDraw()->draw()->drawSoftware()->从DecorView开始递归调用绘制,代码如下:

 mView.draw(canvas);

ViewGroup,FrameLayout中都没有重写此方法,回到View中查看,View中代码也好长,关键是调用了一个diapatchDraw(),在ViewGroup中

重写了这个方法,关键点在于


for (int i = 0; i < childrenCount; i++) {            while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) {                final View transientChild = mTransientViews.get(transientIndex);                if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE ||                        transientChild.getAnimation() != null) {                    more |= drawChild(canvas, transientChild, drawingTime);                }                transientIndex++;                if (transientIndex >= transientCount) {                    transientIndex = -1;                }            }            final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);            final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex);            if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) {                more |= drawChild(canvas, child, drawingTime);            }        }


遍历自己的chlidView一次调用DrawChlid,这个方法如下

protected boolean drawChild(Canvas canvas, View child, long drawingTime) {        return child.draw(canvas, this, drawingTime);    }

调用Chlid中的额draw方法,


draw方法会调用onDraw这个回调,在这个回调中可以自定义View的绘制。终于自上而下完成View的绘制。


其他说明draw方法的说明如下:

/*         * 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)         */


第一步,Draw the background是发生在onDraw之前的,还有剩下的一些画布的处理,应该注意相对回调onDraw的先后的顺序。

原创粉丝点击