Noob to game development and I’m having trouble placing an AnimationDrawable onto a SurfaceView canvas. It’s part of a simple game, user touches screen and an animated gif is placed at that location that looks like an explosion. I can accomplish this with a Bitmap using the code shown below, but converting this to an AnimationDrawable is where I’m stuck. I could create the AnimationDrawable from an ImageView, but I can’t find a way to get the ImageView onto the canvas either…
Am I going about this in the wrong way? Is there a simpler way to get an animated gif to display at an x,y coordinate on a SurfaceView’s canvas?
Bitmap explodeBmp = BitmapFactory.decodeResource(getResources(), R.drawable.explode4);
canvas.drawBitmap(explodeBmp, coords.getX()-(explodeBmp.getWidth()/2), coords.getY()-(explodeBmp.getHeight()/2), paint);
This throws a ClassCastException if I try to convert the Bitmap to an AnimationDrawable and start it:
AnimationDrawable explosionAnimation = (AnimationDrawable) ((Drawable) new BitmapDrawable(explodeBmp));
explosionAnimation.start();
After continuous digging I’ve found the answer… seems I like answering my own questions here.
Just found the Movie class. I can load my animated gif into it using an InputStream, then play the movie bit by bit in my onDraw() because the Movie class supports a draw() method where I can supply my canvas and x,y coordinates.
Here’s the code snippit below: