1

I want to show a jumping bird. I have different images of that bird in different positions. I am using Frame by Frame animation for this. It gives run time exception resource not found.

Here is my code

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();
    }
}

My xml file in 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>

Here is layout->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>

Please help me .what I m doing wrong?

here is my log cat->

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
picaso
  • 713
  • 2
  • 14
  • 26

2 Answers2

0

I know you could use a surface view, and create each frame and display them at set intervals..

You can learn about surfaceviews here: http://developer.android.com/reference/android/view/SurfaceView.html

and I have learned some good info on intervals/looping here: http://obviam.net/index.php/a-very-basic-the-game-loop-for-android/

its discussing a game loop, but you can apply the same knowledge toward a simple animation, and I felt this to be a very nice tutorial.

another way would be to create an animated gif, and get it to display as a video.

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

There are probably other solutions as well.. I'll try to edit this if I think of more.

Community
  • 1
  • 1
snapfractalpop
  • 2,086
  • 2
  • 21
  • 28
  • plz provide me the link so that i can use a surface view and create each frame and display them at set intervals. do u know any tutorial or example to this.Thanks a lot for your response. – picaso Nov 23 '11 at 05:50
0

If you have multiple image and you want to show them one by one with some interval so that they look like Animating then you should Use Frame by Frame Animation for this.

Here you will find Frame by Frame animation example.

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

Arslan Anwar
  • 18,746
  • 19
  • 76
  • 105