动画监听器在使用AnimatorSet时未响应

3

我想让一个动画在另一个动画结束后开始。我使用了animation属性,并将对象定义为"AnimatorSet"。问题是第一个动画没有问题地开始了,但第二个动画从未开始。

public void moveGround() {

        ImageView ground = (ImageView) findViewById(R.id.ground);
        ImageView ground2 = (ImageView) findViewById(R.id.ground2);
        final AnimatorSet moveGround = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.ground_move);
        final Animator moveGround2 = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.ground_move2);
        moveGround2.setTarget(ground2);
        moveGround.setTarget(ground);
        moveGround.start();

        moveGround.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                moveGround2.start();
            }
        });


    }

编辑:我也尝试在动画监听器结束后启动第一个动画:结果相同。

编辑2:XML文件

ground_move.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:ordering="sequentially" >

    <objectAnimator
        android:duration="2000"
        android:propertyName="x"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:valueFrom="2000"
        android:valueTo="-2000"
        android:valueType="floatType"
        />

</set>

ground_move2.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:ordering="sequentially" >

    <objectAnimator
        android:duration="2000"
        android:propertyName="x"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:valueFrom="2000"
        android:valueTo="-2000"
        android:valueType="floatType" />

</set>

1
检查其他方法(onAnimationCancel(),onAnimationPause(),onAnimationRepeat(),onAnimationResume()),打印日志,可能在其他地方被暂停了。 - Alex S. Diaz
啊,是的,当我使用“onAnimationStart()”尝试启动它时,它开始运行。为什么其他方法没有触发呢? - JayRod
添加XML文件 s: - Alex S. Diaz
抱歉回复晚了。我已经添加了XML文件。再次感谢。 - JayRod
1个回答

2

尝试:

moveGround.setTarget(ground);
moveGround.addListener(new AnimatorListenerAdapter() {
    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        moveGround2.start();
    }
});
moveGround.start();

(Note how we first add the listener, then start the animation) 如果这样不起作用,尝试设置一个有限的时间:moveGround.setDuration(sometime); 在启动动画之前检查是否有效地完成了您的动画。 更新 在添加xml文件后,我看到您有android:repeatCount="infinite"android:repeatMode="restart",请尝试删除它们或设置android:repeatCount=0并检查您的监听器是否正确触发。

感谢您的迅速回复。我尝试了这个方法,但它仍然无法正常工作。我还添加了setDuration,但它也不起作用。 :( - JayRod
是的!在我删除了repeatCount(或将其设置为0)并按照您建议的方式删除了repeatMode后,动画就启动了。我如何让第一个再次循环? - JayRod
1
这是一个很好的问题,解决方法是在第二个动画器对象中重新启动您的动画监听器。 - Alex S. Diaz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接