将侦听器设置为帧动画

时间:2013-01-06 21:46:46

标签: android animation listener

我有以下XML文件来定义我的帧动画:

<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/youme_blink_frame_0000" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0001" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0002" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0003" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0004" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0005" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0006" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0007" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0008" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0009" android:duration="40" />
<item android:drawable="@drawable/youme_blink_frame_0010" android:duration="40" />
</animation-list>

然后我有以下代码:

    Animation mAnim = AnimationUtils.loadAnimation(this, R.anim.blink);
    mAnim.setAnimationListener(this);

    ImageView img = (ImageView)findViewById(R.id.animationImage);
    img.clearAnimation();
    img.setAnimation(mAnim);
    img.startAnimation(mAnim);

此代码生成错误,错误“找不到动画文件”。 是框架动画不被认为是动画还是我做错了什么?

谢谢,  西蒙

1 个答案:

答案 0 :(得分:3)

逐帧动画是一个AnimationDrawable而不是动画。你做的是使用它作为动画,这是异常的原因,没有具有该名称的动画文件,有可绘制的文件。 使用AnimationDrawable使用此代码段

// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);

// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

// Start the animation (looped playback by default).
frameAnimation.start();

有关AnimationDrawable的更多信息,请参阅documentation