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

任何帮助将不胜感激。

4

3 回答 3

4

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

于 2020-02-05T12:56:03.323 回答
3

请尝试 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);
    }
});
于 2016-10-11T07:47:41.993 回答
1

助手类

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));
于 2017-12-10T03:02:12.143 回答