ObjectAnimator重复循环延迟

时间:2016-10-11 06:55:09

标签: java android animator

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="rotationX"
    android:valueFrom="0"
    android:valueTo="-360"
    android:repeatCount="infinite"> 
</objectAnimator>

这是我的对象动画xml

 //Animating the NormalLayout
   final ObjectAnimator anim4 = (ObjectAnimator) //Object animator
   AnimatorInflater.loadAnimator(this, R.animator.rotate_four);
//And this is my java code

我希望在循环之间添加延迟,例如再次循环之间的3秒。我已经尝试过动画听众onRepeatonEnd类似的东西,但它没有奏效。我想在循环之间有一些延迟。

任何帮助都将受到极大的欢迎。

3 个答案:

答案 0 :(得分:3)

我发现最好将DelayedAnimationRepeater更改为不重新启动动画,从而允许像期望的那样调用.setRepeatCount(ValueAnimator.INFINITE)和onAnimationRepeat。

因此只需将其更改为

public class DelayedAnimationRepeater implements Animator.AnimatorListener {
    private long delayMillis;

    public DelayedAnimationRepeater(long delayMillis) {
        this.delayMillis = delayMillis;
    }

    @Override
    public void onAnimationStart(Animator animator) { }

    @Override
    public void onAnimationEnd(final Animator animator) { }

    @Override
    public void onAnimationCancel(Animator animator) { }

    @Override
    public void onAnimationRepeat(Animator animator) {
        animator.pause();
        new Handler().postDelayed(animator::resume, delayMillis);
    }
}

可带来更好的体验,恕我直言。现在,您可以真正地重复动画并收听onAnimationRepeat事件。

答案 1 :(得分:0)

请尝试 1.删​​除xml属性android:repeatCount="infinite" 2.添加java代码

anim4.addListener(new AnimatorListenerAdapter(){
    @Override
    public void onAnimationEnd(Animator animation) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                anim4.start();
            }
        }, 3000);
    }
});

答案 2 :(得分:0)

助手类

public class DelayedAnimationRepeater implements Animator.AnimatorListener {
    private long delayMillis;

    public DelayedAnimationRepeater(long delayMillis) {
        this.delayMillis = delayMillis;
    }

    @Override
    public void onAnimationStart(Animator animator) {

    }

    @Override
    public void onAnimationEnd(final Animator animator) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // Start delay should only affect initial start, not repeats
                animator.setStartDelay(0);
                animator.start();
            }
        }, delayMillis);
    }

    @Override
    public void onAnimationCancel(Animator animator) {

    }

    @Override
    public void onAnimationRepeat(Animator animator) {

    }
}

<强>用法

anim4.addListener(new DelayedAnimationRepeater(3000));