今天给大家带来AnimatorSet的联合动画实现,因为疫情原因,高考推迟了一个月,明天就是2020年的高考了,祝各位考生都能上自己理想的学校

  • 概述
  • playTogether:
    • playTogether结论:
  • playSequentially
    • **得出结论:**
  • animatorSet.setTarget()
    • 得出结论:
  • AnimatorSet.Builder
    • 得出结论:
  • 参考文档: [启舰](https://blog.csdn.net/harvic880925/article/details/50759059/).

概述

什么是联合动画?
联合动画就是A执行完动画之后,B在执行动画,B执行完之后,C在执行.或者说ABC同时执行动画等
使用方法:

  • playSequentially 逐个执行

public void playSequentially(Animator… items)
public void playSequentially(List items)

  • playTogether 同时执行

public void playTogether (Animator… items)
public void playTogether (List items)

这两个有参构造方法很好理解,一个是可变参数,一个填的是List集合.

playTogether:

注意:
playTogether() 和 playSequentially() 不能一起使用,否则会没有效果;

  ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);
                AnimatorSet animatorSet = new AnimatorSet();
                //一起播放
                animatorSet.playTogether(tv1TranslationY,Tv1BackgroundColor,tv2TranslationY);

                animatorSet.setDuration(2000);
                animatorSet.start();

从代码中可以看出:
tv1,设置了颜色的变化以及先下平移的动画,tv2设置只设置了向下的平移,并且都执行2s,从效果图中可以看出,和咋们预想的完全一致:

ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);

                tv1TranslationY.setDuration(3000);
                AnimatorSet animatorSet = new AnimatorSet();

                //一起播放
                animatorSet.playTogether(tv1TranslationY,Tv1BackgroundColor,tv2TranslationY);

                animatorSet.setDuration(2000);
                animatorSet.start();

还是上面的代码.只不过给tv1设置了3s的动画时间,现在在来看看效果:

可以从效果图中看出,并没有什么变化,由此得出结论:

  • playTogether()不受其他动画时间的影响.
 ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);
                tv1TranslationY.setStartDelay(3000);//设置开始时间延长3s
                tv1TranslationY.setDuration(3000);
                AnimatorSet animatorSet = new AnimatorSet();

                //一起播放
                animatorSet.playTogether(tv1TranslationY,Tv1BackgroundColor,tv2TranslationY);

                animatorSet.setDuration(2000);
                animatorSet.start();

本次又将开始的时间延长了3s,这次在看看会是什么效果:

从图中可以很明显的看出,将开始时间延长3s(setStartDelay(3000)),确实起作用了的出新结论:
setStartDelay()可以影响playTogether()的启动时间

ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);
                tv1TranslationY.setStartDelay(3000);
                tv1TranslationY.setRepeatCount(ObjectAnimator.INFINITE);
                AnimatorSet animatorSet = new AnimatorSet();
                //一起播放
                animatorSet.playTogether(tv1TranslationY,Tv1BackgroundColor,tv2TranslationY);

                animatorSet.setDuration(2000);
                animatorSet.start();

本次又将tv1TranslationY设置了无限次数的重复,来看看效果他会不会一直重复呢?

从效果图冲可以看出,setRepeatCount也不受playTogether()的影响:

playTogether结论:

  • playTogether()不受其他动画时间的影响.
  • setStartDelay()可以影响playTogether()的启动时间
  • setRepeatCount()不受playTogether()的影响

playSequentially

ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 
                AnimatorSet animatorSet = new AnimatorSet();
                //逐个播放
                animatorSet.playSequentially(tv1TranslationY,Tv1BackgroundColor,tv2TranslationY);
                animatorSet.setDuration(2000);
                animatorSet.start();

本次设置的是逐个播放,就是按顺序播放动画.

可以得出结论,执行的顺序就是
animatorSet.playSequentially(tv1TranslationY,Tv1BackgroundColor,tv2TranslationY);设置的顺序.

下一步还是按照上面测试playTogether()的方法测试playSequentially()

  ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);
// tv1TranslationY.setStartDelay(3000);
                tv1TranslationY.setDuration(3000);
                AnimatorSet animatorSet = new AnimatorSet();
                //逐个播放
                animatorSet.playSequentially(tv1TranslationY,Tv1BackgroundColor,tv2TranslationY);
                animatorSet.setDuration(2000);
                animatorSet.start();

本次还是正常的设置,并且给tv1设置了3s的动画时间.

可以看出,playTogether()一样,并没有什么效果;

ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);
               tv1TranslationY.setStartDelay(3000);
                AnimatorSet animatorSet = new AnimatorSet();
                //逐个播放
                animatorSet.playSequentially(tv1TranslationY,Tv1BackgroundColor,tv2TranslationY);


                animatorSet.setDuration(2000);
                animatorSet.start();

本次还是将tv1的开始时间延长3s

ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);
                tv1TranslationY.setStartDelay(3000);
                tv1TranslationY.setRepeatCount(ObjectAnimator.INFINITE);
// tv1TranslationY.setDuration(3000);
                AnimatorSet animatorSet = new AnimatorSet();
                //逐个播放
                animatorSet.playSequentially(Tv1BackgroundColor,tv1TranslationY,tv2TranslationY);

                animatorSet.setDuration(2000);
                animatorSet.start();

本次还是设置tv1TranslationY的setRepeatCount()为无限次数播放,来康康效果:

可以很清晰的看到先执行了tv1的颜色变化,然后停留了3s,执行了tv1的移动动画,tv2始终没有被执行到;

得出结论:

  • playSequentially()的动画执行顺序是根据参数所执行的
  • 设置动画时间会不起作用
  • 延长动画时间起作用
  • 若要给动画设置了无限次数执行的操作,则该操作后面的动画会执行不到,因为前面不执行完,后面没办法执行

animatorSet.setTarget()

设置ObjectAnimator动画目标控件
public void setTarget(Object target)

  • playTogether:
 ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationX", 0, 100, 300);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300);
                AnimatorSet animatorSet = new AnimatorSet();
                //逐个播放
                animatorSet.playSequentially(Tv1BackgroundColor,tv1TranslationY,tv2TranslationY);
                animatorSet.setTarget(tv2);
                animatorSet.setDuration(2000);
                animatorSet.start();

将所有动画都给到tv2,包括tv1的X轴移动,颜色变化以及本身的Y轴移动,并依次播放,来看看效果吧:

playSequentially:

 ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationX", 0, 100, 300);
                ObjectAnimator Tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300);
                AnimatorSet animatorSet = new AnimatorSet();
                //逐个播放
                animatorSet.playSequentially(Tv1BackgroundColor,tv1TranslationY,tv2TranslationY);
                animatorSet.setTarget(tv2);
                animatorSet.setDuration(2000);
                animatorSet.start();

本次设置了逐个播放,给到了tv2控件,来看看效果吧:

可以看出,还是吧所以的动画都给到了tv2,并且依次播放

得出结论:

  • 会同时把所有的动画效果都集中到一个控件’身上’,并且保留最后状态playTogether会同时播放playSequentially会逐个播放

AnimatorSet.Builder

//调用AnimatorSet中的play方法是获取AnimatorSet.Builder对象的唯一途径
//表示要播放哪个动画
public Builder play(Animator anim)

表示一起执行的动画,不能after一起执行
public Builder with(Animator anim)
表示最后执行的动画,若有多个,多个一起执行
public Builder before(Animator anim)
表示最先执行的动画,若有多个,多个一起执行
public Builder after(Animator anim)
//延迟n毫秒之后执行动画
public Builder after(long delay)

动画执行优先级:
after() > play() && with() > before()
 ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);
                ObjectAnimator tv3TranslationY = ObjectAnimator.ofFloat(tv3, "translationY", 0, 100, 300, 0);

                AnimatorSet animatorSet = new AnimatorSet();
                AnimatorSet.Builder play = animatorSet.play(tv1TranslationY);
                play.before(tv2TranslationY).after(tv3TranslationY).with(tv1BackgroundColor);
                animatorSet.setDuration(3000);
                animatorSet.start();

从这行代码可以看出,应该是先执行tv3TranslationY的动画,紧接着执行tv1TranslationY和tv1BackgroundColor的动画,最后在执行tv2TranslationY的动画,来看看效果吧:

ObjectAnimator tv1TranslationY = ObjectAnimator.ofFloat(tv1, "translationY", 0, 100, 300, 0);
                ObjectAnimator tv1BackgroundColor = ObjectAnimator.ofInt(tv1, "BackgroundColor", 0xff00ff11, 0xff22ff11, 0xff000011, 0xff019f11);
                ObjectAnimator tv2TranslationY = ObjectAnimator.ofFloat(tv2, "translationY", 0, 100, 300, 0);
                ObjectAnimator tv3TranslationY = ObjectAnimator.ofFloat(tv3, "translationY", 0, 100, 300, 0);

              tv1TranslationY.setRepeatCount(-1);
                tv3TranslationY.setStartDelay(3000);

                AnimatorSet animatorSet = new AnimatorSet();
                AnimatorSet.Builder play = animatorSet.play(tv1TranslationY);
                play.before(tv2TranslationY).after(tv3TranslationY).with(tv1BackgroundColor);
                animatorSet.setDuration(3000);

本次给tv1设置了无限次数播放的操作,并且给tv3设置了延迟3s开启动画的操作,来看看效果吧:

可以看出,tv3 3s后执行了Y轴移动的动画,并且Tv1无限的执行;

得出结论:

  • 延长动画时间起作用
  • 若要给动画设置了无限次数执行的操作,则该操作后面的动画会执行不到,因为前面不执行完,后面没办法执行

参考文档: 启舰.

git地址:链接: langyangyang.

本文地址:https://blog.csdn.net/weixin_44819566/article/details/107150833