本文介绍了通过XML定义AnimationSet不重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的XML code,应模拟一种心脏跳动的动画。使用它应该扩展两次,并返回到原始大小的图像,然后再次重新启动:

I have this xml code, that should simulate a sort of "heart beat" animation. Image that use it should scale twice, and return to original size, then restart again:

<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:

and this is how i add this animation set to my imageview:

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

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

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

but animation is executed only one time, then it stops. Why?

推荐答案

编辑:

根据的文档sintax,这些属性必须被设置为Animatior,而不是设定,让我们有那么这个自定义动画没有code重复

According to the documentation sintax of Animation, these properties must be set to an Animatior and not to the set, so let's have then this custom animation repeating with no code:

<?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>

这篇关于通过XML定义AnimationSet不重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:58