如何使用顺序添加的动画重复AnimationSet [英] How to repeat an AnimationSet with sequentially added animations

查看:192
本文介绍了如何使用顺序添加的动画重复AnimationSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的Activity中制作动画,以使其无限次重复.我已经在具有repeatCountrepeatMode属性的XML文件中尝试过它,但是它不起作用.事实是myanimation.xml文件是由一组不同的动画构成的.

I am trying to make an animation in my Activity, to be repeated infinite times. I have already tried it in XML file with repeatCount and repeatMode attributes, but it doesn't work. The thing is that myanimation.xml file is constructed of a set of different animations.

该动画的我的XML文件

My XML file for that animation

<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/bounce_interpolator"
>



<translate


    android:startOffset="1000"
    android:fillAfter="true"
    android:fromXDelta="10"
    android:fromYDelta="10"
    android:toXDelta="50"
    android:toYDelta="-200"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator"

    />



<translate

    android:fillAfter="true"
    android:startOffset="2000"
    android:fromYDelta="10"
    android:fromXDelta="10"
    android:toXDelta="100"
    android:toYDelta="270"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator" />

<translate

    android:fillAfter="true"
    android:startOffset="3000"
    android:fromYDelta="10"
    android:fromXDelta="10"
    android:toXDelta="130"
    android:toYDelta="-270"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator"
    />

<translate

    android:fillAfter="true"
    android:startOffset="4000"
    android:fromYDelta="10"
    android:fromXDelta="10"
    android:toXDelta="140"
    android:toYDelta="270"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator"
    />

<translate

    android:fillAfter="true"
    android:startOffset="5000"
    android:fromYDelta="10"
    android:fromXDelta="10"
    android:toXDelta="90"
    android:toYDelta="-270"
    android:duration="1800"
    android:interpolator="@android:anim/bounce_interpolator"
    />

onCreate()中,我将动画绑定到ImageView对象.

And in onCreate() I have animation tied to a ImageView object.

    ImageView ball = (ImageView) findViewById(R.id.animationBall);
    final Animation myAnimation = AnimationUtils.loadAnimation(this,   R.anim.ball_animation);
    ball.startAnimation(myAnimation);

动画效果很好,唯一的是即使我设置了setRepeatMode()setRepeatCount()方法,它也不想重复自己.

The animation works fine, the only thing is that it doesn't want to repeat itself, even if I set the setRepeatMode() or setRepeatCount() methods.

推荐答案

对于其价值,必须在Animation对象而不是AnimationSet对象上设置setRepeatMode()setRepeatCount().这可能是您可能犯的一个错误.

For what its worth, setRepeatMode() and setRepeatCount() have to be set on the Animation objects, and not on the AnimationSet object. That's potentially a mistake you may have made.

因此,要么在Animation对象上调用这些方法,要么将那些属性添加到translate架构的XML中.

So either call those methods on the Animation objects or add those attributes to the XML of the translate schema.

另一种方法是如下设置不断重复的动画:

Another approach is to set an endlessly repeating animation as follows:

mAnimationSet.addListener(new AnimatorListenerAdapter() {

    @Override
    public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        mAnimationSet.start();
    }

});
mAnimationSet.start();

这篇关于如何使用顺序添加的动画重复AnimationSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆