1

我有以下 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);

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

谢谢,西蒙

4

1 回答 1

3

逐帧动画是 AnimationDrawable 而不是 Animation。您所做的是将它用作动画,这就是异常的原因没有具有该名称的动画文件有可绘制文件。使用 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 的更多信息,请参阅文档

于 2013-02-09T01:57:49.620 回答