AnimatorSet.setStartDelay在Android4.4上运行问题

在Android4.4上,当使用AnimatorSet.setStartDelay配合多个动画时,实际效果可能与预期不符,出现4秒延迟后两个动画同时开始。原因是AnimatorSet内部的依赖关系处理,导致了动画的启动顺序与设定的延迟时间产生冲突。通过对源码的分析,揭示了动画启动时机的关键在于DependencyListener的onAnimationStart回调,它在第一个动画开始时启动第二个动画,从而造成总延迟时间等于两者之和。
摘要由CSDN通过智能技术生成

先上代码:

ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(textView1, "translationY", 0, 200, 0);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(textView2, "translationY", 0, 200, 0);
objectAnimator1.setStartDelay(2000);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(objectAnimator1).with(objectAnimator2);
animatorSet.setStartDelay(2000);
animatorSet.setDuration(2000);
animatorSet.start();

简单的两个TextView控件位移动画,放在一个AnimatorSet中调用play,with一起执行,第一个动画setStartDelay(2000);第二个动画没有设置开始延迟,然后在设置AnimatorSet.setStartDelay(2000);调用start开始,实际运行结果和预想的不太一样,预想结果是动画开始后先延迟2秒然后第二动画执行2秒后第一个动画才开始执行,实际运行结果为延迟4s后,两个TextView控件同时进行位移动画后结束。

这里调用animatorSet.play(objectAnimator1).with(objectAnimator2);和调用animatorSet.playTogether本质上一样,因为通过看源码playTogether也是通过play和with方法实现的。
playTogether方法实现:

    public void playTogether(Animator... items) {
        if (items != null) {
            Builder builder = play(items[0]);
            for (int i = 1; i < items.length; ++i) {
                builder.with(items[i]);
            }
        }
    }

因为结果与预想不同,于是来看一下源码到底为什么会出现这个效果。
看play方法:

  public Builder play(Animator anim) {
        if (anim != null) {
            mNeedsSort = true;
            return new Builder(anim);
        }
        return null;
    }

play方法中直接返回一个Builder对象。接着看Builder的构造函数:

     Builder(Animator anim) {
            mCurrentNode = mNodeMap.get(anim);
            if (mCurrentNode == null) {
                mCurrentNode = new Node(anim);
                mNodeMap.put(anim, mCurrentNode);
                mNodes.add(mCurrentNode);
            }
        }

AnimatorSet中的mNodeMap和mNodes用来存放所有节点,mNodeMap的key是animator,value是node。将传入的动画封装成一个Node加入集合中:

    /**
     * Contains all nodes, mapped to their respective Animators. When new
     * dependency information is added for an Animator, we want to add it
     * to a single node representing that Animator, not create a new Node
     * if one already exists.
     */
    private HashMap<Animator, Node> mNodeMap = new HashMap<Animator, Node>();

    /**
     * Set of all nodes created for this AnimatorSet. This list is used upon
     * starting the set, and the nodes are placed in sorted order into the
     * sortedNodes collection.
     */
    private ArrayList<Node> mNodes = new ArrayList<Node>();

Builder中mCurrentNode表示当前动画的节点:

       /**
         * This tracks the current node being processed. It is supplied to the play() method
         * of AnimatorSet and passed into the constructor of Builder.
         */
        private Node mCurrentNode;

接下来看with方法:

  public Builder with(Animator anim) {
            Node node = mNodeMap.get(anim);
            if (node == null) {
                node = new Node(anim);
                mNodeMap.put(anim, node);
                mNodes.add(node);
            }
            Dependency dependen
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值