为什么逐帧动画不能正常工作?

时间:2011-11-23 05:40:01

标签: android animation

我想展示一只跳鸟。我有不同位置的那只鸟的不同图像。我正在使用逐帧动画。它提供了未找到的运行时异常资源。

这是我的代码

public class AnimationDrawableActivity extends Activity {

    ImageView im;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation);

        im=(ImageView)findViewById(R.id.imageView_hostanimation);
        im.setBackgroundResource(R.drawable.spain_animation);
        AnimationDrawable frameAnimation = (AnimationDrawable) im.getBackground();
        frameAnimation.setCallback(im);
        frameAnimation.setVisible(true, true);

        frameAnimation.start();
    }
}

我的xml文件在drawable-mdpi-> spain_animation.xml

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

    <animation-list
        android:id="@+id/testanimation" android:oneshot="false" >

        <item
            android:drawable="@drawable/icon" android:duration="150"/>
        <item
            android:drawable="@drawable/pokeslidebird1" android:duration="150"/>
        <item
            android:drawable="@drawable/pokeslidebird2" android:duration="150"/>
        <item
            android:drawable="@drawable/icon" android:duration="150"/>
        <item
            android:drawable="@drawable/pokeslidebird1" android:duration="150"/>
        <item
            android:drawable="@drawable/icon" android:duration="150"/>
    </animation-list>
</animated-rotate>

这是layout-&gt; animation.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" >

        <ImageView
            android:id="@+id/imageView_hostanimation"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="56dp"
            android:layout_marginTop="70dp"
            android:src="@drawable/icon" >
        </ImageView>
    </RelativeLayout>

</RelativeLayout>

请帮助我。我做错了什么?

这是我的log cat-&gt;

11-23 12:45:35.070: ERROR/AndroidRuntime(517): Uncaught handler: thread main exiting due to uncaught exception
11-23 12:45:35.100: ERROR/AndroidRuntime(517): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.a3.activity/com.a3.activity.AnimationDrawableActivity}: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/spain_animation.xml from drawable resource ID #0x7f020003
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.os.Looper.loop(Looper.java:123)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.main(ActivityThread.java:4363)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at java.lang.reflect.Method.invoke(Method.java:521)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at dalvik.system.NativeStart.main(Native Method)
11-23 12:45:35.100: ERROR/AndroidRuntime(517): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-mdpi/spain_animation.xml from drawable resource ID #0x7f020003
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.content.res.Resources.loadDrawable(Resources.java:1693)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.content.res.Resources.getDrawable(Resources.java:580)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.view.View.setBackgroundResource(View.java:7186)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at com.a3.activity.AnimationDrawableActivity.onCreate(AnimationDrawableActivity.java:17)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     ... 11 more
11-23 12:45:35.100: ERROR/AndroidRuntime(517): Caused by: java.lang.NullPointerException
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.graphics.drawable.AnimatedRotateDrawable.inflate(AnimatedRotateDrawable.java:222)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     at android.content.res.Resources.loadDrawable(Resources.java:1690)
11-23 12:45:35.100: ERROR/AndroidRuntime(517):     ... 16 more

2 个答案:

答案 0 :(得分:0)

我知道您可以使用表面视图,并创建每个帧并以设定的间隔显示它们。

您可以在此处了解表面视图:http://developer.android.com/reference/android/view/SurfaceView.html

我在这里学习了一些有关间隔/循环的好信息:http://obviam.net/index.php/a-very-basic-the-game-loop-for-android/

它讨论游戏循环,但你可以将相同的知识应用于一个简单的动画,我觉得这是一个非常好的教程。

另一种方法是创建动画gif,并将其显示为视频。

Android: How do a display a large animated gif given a url?

也可能有其他解决方案..如果我想到更多的话,我会尝试编辑它。

答案 1 :(得分:0)

如果您有多个图像,并且希望逐个显示它们,使它们看起来像动画,那么您应该使用逐帧动画。

在这里,您将找到逐帧动画示例。

http://developer.android.com/reference/android/graphics/drawable/AnimationDrawable.html