0

I have 16 images that represent someone throwing a ball. I want to make a fluid animation with these images. With 20 images/s the eye thinks it's a movement and not a succession of images. So I want to display 1 image every 50ms

public void fire() {
final ImageView image3 = (ImageView) gameactivity.findViewById(R.id.imageView3);
final int drawables[] = new int[] {R.drawable.fire1,R.drawable.fire2,R.drawable.fire3,R.drawable.fire4,R.drawable.fire5,R.drawable.fire6,R.drawable.fire7,R.drawable.fire8,R.drawable.fire9,R.drawable.fire10,R.drawable.fire11,R.drawable.fire12,R.drawable.fire13,R.drawable.fire14,R.drawable.fire15,R.drawable.fire16};
for (int i=0;i<drawables.length;i++) {
    final int j=i;
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            image3.setImageResource(drawables[j]);
            Log.w("GAMEACTIVITY","image"+j);
        }
    };
    gameactivity.handler.postDelayed(runnable, 50*j);
  }
}

But the animation is not fluid I see the first image, then nothing and then the last one, probably because 50ms is too short to make the difference between the action of the Runnables?

EDIT Using animation drawable

ImageView image2 = (ImageView) findViewById(R.id.imageView3);
        image2.setBackgroundResource(R.drawable.fire);
        AnimationDrawable fireAnimation = (AnimationDrawable) image2.getBackground();
        image2.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                fireAnimation.start();
                return false;
            }

I have another memory error. It's not the first time that happens and I really don't know how to handle it (read a lot about memory leaks but I don't think it's that)

02-12 10:20:30.279: E/dalvikvm-heap(1349): 1535664-byte external allocation too large for this process.
02-12 10:20:30.319: E/GraphicsJNI(1349): VM won't let us allocate 1535664 bytes
02-12 10:20:30.319: W/dalvikvm(1349): threadid=1: thread exiting with uncaught exception (group=0x40018560)
02-12 10:20:30.329: E/AndroidRuntime(1349): FATAL EXCEPTION: main
02-12 10:20:30.329: E/AndroidRuntime(1349): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.content.res.Resources.loadDrawable(Resources.java:1709)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.content.res.Resources.getDrawable(Resources.java:581)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:267)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:787)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:728)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.content.res.Resources.getDrawable(Resources.java:581)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.view.View.setBackgroundResource(View.java:7533)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at com.example.kersplatt.GameActivity.onCreate(GameActivity.java:69)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.os.Looper.loop(Looper.java:130)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at java.lang.reflect.Method.invoke(Method.java:507)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
02-12 10:20:30.329: E/AndroidRuntime(1349):     at dalvik.system.NativeStart.main(Native Method)
morg
  • 1,173
  • 4
  • 18
  • 36

2 Answers2

1

If you have a bunch of images, You should check out Android Drawable Animation instead of using runnables.

It allows you define a set of drawables and the duration that each one should be displayed for.

It is supported natively by the platform and will be much more simple and pain free to implement.

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
0

You need to preload the images otherwise the setImageResource() will load (read from storage) the image at each call which should take longer than 50ms (I guess your images are not just 20x20)

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150