如何将animation-list设置为xml属性

时间:2012-11-26 08:47:40

标签: android android-animation android-xml

有没有办法设置和启动xml animation-list作为xml属性?我可以按照以下方式以编程方式设置和启动它:

    ImageView img = (ImageView) findViewById(R.id.loadingImageView);
    img.setBackgroundResource(R.drawable.loading_animation);
    AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
    frameAnimation.start();

动画列表是:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
//...
android:oneshot="false" >

<item
    android:drawable="@drawable/loading_anim_frame_one"
    android:duration="50"/>
 <item
    android:drawable="@drawable/loading_anim_frame_two"
    android:duration="50"/>

等等。

有没有办法只使用xml标记,即没有java代码?

如果没有,是否有办法至少将其设置为xml属性,然后以编程方式启动它?

我无法使用单个drawable的旋转,因为动画按顺序包含多个drawable。

3 个答案:

答案 0 :(得分:16)

您可以将其声明为

<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="700"
        android:fillAfter="false"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.4"
        android:toYScale="1.4" />

    <scale
        android:duration="400"
        android:fillBefore="false"
        android:fromXScale="1.4"
        android:fromYScale="1.4"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="700"
        android:toXScale="0.8"
        android:toYScale="0.8" />

</set>

仅供参考。您必须更改动画类型和参数。据我所知,你必须使用java开始它。

编辑:

This是对动画列表有用的链接

答案 1 :(得分:5)

我认为你应该首先加载动画:

Animation anim = AnimationUtils.loadAnimation(this, R.anim.animation_name);

img.startAnimation(anim);

答案 2 :(得分:0)

添加带有alpha和翻译的示例以供将来参考

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="400"
        android:fromYDelta="-100%p"
        android:toYDelta="0%p" />

    <alpha
        android:duration="800"
        android:fromAlpha="0"
        android:toAlpha="1" />
</set>
相关问题