ObjectAnimator 重复循环延迟

ObjectAnimator repeat loop delay

<?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 之类的东西,但没有用。我想要在循环之间有一些延迟。

任何帮助将不胜感激。

请试试 1.delete xml 属性 android:repeatCount="infinite" 2.add java 代码

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

帮手class:

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));

我发现最好将 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 事件。