Android动画执行过程源码分析

本文详细分析了Android中View.startAnimation的执行流程,从View开始,经过ViewGroup,再到ViewRootImpl,深入探讨了动画如何影响视图的绘制过程,包括Choreographer在其中的作用以及如何调度动画帧。通过对源码的解读,揭示了动画执行过程中的关键步骤,如drawAnimation、invalidateChildInParent等,帮助理解Android动画机制。
摘要由CSDN通过智能技术生成
1、View.startAnimation
    public void startAnimation(Animation animation) {
        animation.setStartTime(Animation.START_ON_FIRST_FRAME);
        setAnimation(animation);
        invalidateParentCaches();
        invalidate(true);
    }

setAnimation 将mCurrentAnimation设置为当前要执行的animation
 
 
 
invalidateParentCaches将当前view的mParent的flag设为 PFLAG_INVALIDATED
然后调用invalidate,同时参数为true,使当前view的缓存失效

2、View.invalidate
    // Propagate the damage rectangle to the parent view.
    final AttachInfo ai = mAttachInfo;
    final ViewParent p = mParent;
    if (p != null && ai != null && l < r && t < b) {
        final Rect damage = ai.mTmpInvalRect;
        damage.set(l, t, r, b);
        p.invalidateChild(this, damage);
    }
这里会将失效的矩阵传递给ViewParent,ViewParent的直接子类有ViewGroup和ViewRootImpl,通常情况下,view的直接parent是ViewGroup,所以接下来会进入ViewGroup.invalidateChild
3、ViewGroup.invalidateChild
在这个方法中
            final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION)
                    == PFLAG_DRAW_ANIMATION;
            do {
                View view = null;
                if (parent instanceof View) {
                    view = (View) parent;
                }
                ...
                if (drawAnimation) {
                    if (view != null) {
                        view.mPrivateFlags |= PFLAG_DRAW_ANIMATION;
                    } else if (parent instanceof ViewRootImpl) {
                        ((ViewRootImpl) parent).mIsAnimating = true;
                    }
                }
                // ...
                parent = parent.invalidateChildInParent(location, dirty);
            } while (parent != null);
这里会循环调用parent的invalidateChildInParent方法,在ViewGroup.invalidateChildInParent中,会返回它的mParent,但是最下层的ViewRootImpl.invalidateChildI
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值