AnimatorSet组合动画

  • 看了ValueAnimator和ObjectAnimator,他们只能实现一个动画,如果我们想使用一个组合动画,就需要用到AnimatorSet

  • 一般而言我们不会使用ValueAnimator,只会使用ObjectAnimator组合动画实现

  • 在AnimatorSet中提供了两个函数:playSequentially()和playTogether(),前者表示所用动画依次播放,后者表示动画一起播放

  • playSequentially()函数:

public void playSequentially(Animator... items);
public void playSequentially(List<Animator> items);

eg:

private void doAnimator() {
    ObjectAnimator objectAnimator1 = ObjectAnimator.ofInt(textView1,"BackgroundColor", 0xffff00ff, 0xffffff00,0xffff00ff);
    ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(textView1,"translationY",0 , 300, 0);
    ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(textView2, "translationY", 0, 400, 0);
    AnimatorSet animatorSet = new AnimatorSet();
    //依次执行
    animatorSet.playSequentially(objectAnimator1,objectAnimator2,objectAnimator3);
    animatorSet.setDuration(1000);
    animatorSet.start();
}
  • playTogether()函数:
public void playTogether(Animator... items);
public void playTogether(Collection<Animator> items);

eg;

private void doAnimator() {
    ObjectAnimator objectAnimator1 = ObjectAnimator.ofInt(textView1,"BackgroundColor", 0xffff00ff, 0xffffff00,0xffff00ff);
    ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(textView1,"translationY",0 , 300, 0);
    ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(textView2, "translationY", 0, 400, 0);
    AnimatorSet animatorSet = new AnimatorSet();
    //一起执行
    animatorSet.playTogether(objectAnimator1, objectAnimator2, objectAnimator3);
    animatorSet.setDuration(1000);
    animatorSet.start();
}
  • 两者比较:
    • playTogether函数指的是在同一时间点,至于动画是否结束,以及何时结束都不管。
    • playSequentially函数当执行一个动画的时候,必须等待该动画执行完毕才会执行下一个动画
      • 通过下面的例子进行更详细的了解,在中间的控件上设置一个无线循环播放
  • playTogether()效果是等到颜色变换后两个TextView做平移变换,变换后中间的控件还继续进行无限循环播放
private void doAnimator() {
    ObjectAnimator objectAnimator1 = ObjectAnimator.ofInt(textView1,"BackgroundColor", 0xffff00ff, 0xffffff00,0xffff00ff);
    ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(textView1,"translationY",0 , 300, 0);
    //延时2秒,循环无穷次
    objectAnimator2.setStartDelay(2000);
    objectAnimator2.setRepeatCount(ValueAnimator.INFINITE);
    ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(textView2, "translationY", 0, 400, 0);
    objectAnimator3.setStartDelay(2000);
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playTogether(objectAnimator1, objectAnimator2, objectAnimator3);
    animatorSet.setDuration(2000);
    animatorSet.start();
}
  • playSequentially()效果是,第三个动画永远无法播放,因为中间的无限循环,所以第三个一直被阻塞

private void doAnimator() {
    ObjectAnimator objectAnimator1 = ObjectAnimator.ofInt(textView1,"BackgroundColor", 0xffff00ff, 0xffffff00,0xffff00ff);
    ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(textView1,"translationY",0 , 300, 0);
    //延时2秒,循环无穷次
    objectAnimator2.setStartDelay(2000);
    objectAnimator2.setRepeatCount(ValueAnimator.INFINITE);
    ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(textView2, "translationY", 0, 400, 0);
    objectAnimator3.setStartDelay(2000);
    AnimatorSet animatorSet = new AnimatorSet();
    animatorSet.playSequentially(objectAnimator1, objectAnimator2, objectAnimator3);
    animatorSet.setDuration(2000);
    animatorSet.start();
}
  • AnimatorSet.Builder
    • 为了解决无法自定义动画的播放顺序,Google提该类
//表示要播放哪一个动画
public Builder play(Animator anim);
//和前面的动画一起执行
public Builder with(Animator anim);
//先执行这个动画,再执行前面的动画
public Builder before(Animator anim);
//在执行前面的动画后才执行该动画
public Builder after(Animator anim);
//延迟n毫秒之后执行动画
public Builder after(long delay);
  • before after等函数都是以当前播放的动画为基准
private void doAnimator() {
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofInt(textView1,"BackgroundColor", 0xffff00ff, 0xffffff00,0xffff00ff);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(textView1,"translationY",0 , 300, 0);
        //延时2秒,循环无穷次
        objectAnimator2.setStartDelay(2000);
        objectAnimator2.setRepeatCount(ValueAnimator.INFINITE);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(textView2, "translationY", 0, 400, 0);
        objectAnimator3.setStartDelay(2000);
        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.playSequentially(objectAnimator1, objectAnimator2, objectAnimator3);
//        animatorSet.setDuration(2000);
//        animatorSet.start();
        AnimatorSet.Builder builder = animatorSet.play(objectAnimator1);
        builder.with(objectAnimator2);
        //在执行完obj3之后在执行上面的动画
        builder.after(objectAnimator3);
        animatorSet.setDuration(1000);
        animatorSet.start();
    }
  • Animator.Builder使用方法:
    • 逐个添加(与上面的例子相同)
    • 串行方式(生成器模式)
animatorSet.play(objectAnimator1).with(objectAnimator).after(objectAnimator3);
  • AnimatorSet监听器:
public static interface AnimatorListener{
    /**
     * 当AnimatorSet开始调用
   */
    void onAnimatorStart(Animator animator);
    /**
     * 当AnimatorSet结束时调用
     */
    void onAnimationEnd(Animator animator);


    /**
     * 当AnimatorSet取消时调用
     */
    void onAnimationCancel(Animator animator);


    /**
     * 当AnimatorSet重复时调用
     */
    void onAnimationRepeat(Animator animator);
}
  • 使用方式:
animatorSet.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {
        System.out.println("start");
    }


    @Override
    public void onAnimationEnd(Animator animation) {


    }


    @Override
    public void onAnimationCancel(Animator animation) {


    }


    @Override
    public void onAnimationRepeat(Animator animation) {


    }
});
  • 注意:AnimatorSet的监听函数只用来监听AnimatorSet的状态,与其中的动画无关,并且就算里面有重复的动画,他也永远无法执行onAnimationRepeat()函数

  • 常用函数:

    • 在AnimatorSet中还有如下几个函数:
//设置单次动画时长
public AnimatorSet setDuration(long duration);
//设置插值器
public void setInterpolator(TimeInterpolator interpolator);
//设置ObjectAnimator动画目标控件,只要通过该函数设置目标控件,那么单个动画中的目标控件都是以AnimatorSet为准
public void setTarget(Object target)
  • 上述的函数在ObjectAnimator中也存在,但是如果在AnimatorSet中设置之后,会覆盖单个ObjectAnimator中的设置
  • 例外的一个函数是setStartDelay(long startDelay)函数,它是用来设置AnimatorSet的激活时间的延时,单个动画的延时仍然有效。
    • 注意
      • AnimatorSet的真正的延时时间是本身设置的延时时间加上第一个单个动画的延时时间。
      • AnimatorSet被激活后第一个动画肯定被执行,但是第二个动画可以根据自己的延时进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wjxbless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值