通过XML定义的AnimationSet不重复

时间:2014-03-16 10:53:06

标签: android xml android-animation

我有这个xml代码,应该模拟一种“心跳”动画。使用它的图像应缩放两次,然后返回原始大小,然后重新启动:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:repeatCount="-1"
android:repeatMode="restart"
android:shareInterpolator="true" >

<scale
    android:duration="500"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.2"
    android:toYScale="1.2" />
<scale
    android:duration="500"
    android:fromXScale="1.2"
    android:fromYScale="1.2"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.4"
    android:toYScale="1.4" />
<scale
    android:duration="500"
    android:fromXScale="1.4"
    android:fromYScale="1.4"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0" />

</set>

这就是我将这个动画集添加到我的imageview的方式:

AnimationSet heart_pulse = new AnimationSet(true);
            heart_pulse.addAnimation(AnimationUtils.loadAnimation(activity,
                    R.anim.pulsexml));

            logo.setAnimation(heart_pulse);
            logo.startAnimation(heart_pulse);

但动画只执行一次,然后停止。为什么呢?

3 个答案:

答案 0 :(得分:4)

试试这个自定义插值器:

final ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.heart);
setContentView(iv);
Runnable action = new Runnable() {
    @Override
    public void run() {
        Interpolator i = new Interpolator() {
            @Override
            public float getInterpolation(float input) {
                float x = input < 1/3f? 2 * input : (1 + input) / 2;
                return (float) Math.sin(x * Math.PI);
            }
        };
        ScaleAnimation anim = new ScaleAnimation(1, 1.2f, 1, 1.2f, iv.getWidth() / 2, iv.getHeight() / 2);
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(750);
        anim.setInterpolator(i);
        iv.startAnimation(anim);
    }
};
iv.post(action);

答案 1 :(得分:3)

您是否尝试过添加android:fillAfter="false"

修改

根据Animation的文档sintax,这些属性必须设置为Animatior而不是设置,所以让我们自定义动画重复无代码

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale
        android:duration="500"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:toXScale="1.2"
        android:toYScale="1.2" />
    <scale
        android:duration="500"
        android:fromXScale="1.2"
        android:fromYScale="1.2"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:toXScale="1.4"
        android:toYScale="1.4" />
    <scale
        android:duration="500"
        android:fromXScale="1.4"
        android:fromYScale="1.4"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

答案 2 :(得分:1)

考虑为动画使用CycleInterpolator,例如:

final Animation animation = AnimationUtils.loadAnimation(context, R.anim.pulsexml);
animation.setInterpolator(new CycleInterpolator(3f));
logo.startAnimation(animation);