Android中View绘制过程(六) performDraw

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的先后的顺序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值