How to place animation on SurfaceView canvas

Noob for game development, and I am unable to place AnimationDrawable on SurfaceView canvas. This is part of a simple game, the user touches the screen and the animated gif is placed in a place that looks like an explosion. I can accomplish this using Bitmap using the code below, but converting this to AnimationDrawable is where I got stuck. I could create an AnimationDrawable from an ImageView, but I can't find a way to get an ImageView on canvas either ...

Am I really wrong about that? Is there an easier way to get an animated gif to display in x, y coordinate on SurfaceView 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 Bitmap to AnimationDrawable and fire it:

 AnimationDrawable explosionAnimation = (AnimationDrawable) ((Drawable) new BitmapDrawable(explodeBmp)); explosionAnimation.start(); 
+4
source share
1 answer

After continuous digging, I found the answer ... I seem to like to answer my questions here.

Just found a movie class. I can load my animated gif into it using InputStream, and then play the movie bit by bit in my onDraw (), because the Movie class supports the draw () method, where I can provide my canvas and x, y coordinates.

Below is the code snippet below:

 InputStream is = context.getResources().openRawResource(R.drawable.dotz_explosion); Movie explodeGif = Movie.decodeStream(is); ... @Override protected void onDraw(Canvas canvas) { ... GraphicObject explosion = (GraphicObject)ex.next(); long now = android.os.SystemClock.uptimeMillis(); if (explosion.getMovieStart() == 0) { // first time explosion.setMovieStart(now); } int relTime = (int)((now - explosion.getMovieStart()) % explodeGif.duration()); if ((now - explosion.getMovieStart()) >= explodeGif.duration()) { removeArrayExplosions.add(removeIndex); explosion.setMovieStart(0); } else { explodeGif.setTime(relTime); explodeGif.draw(canvas, explosion.getX()-(explodeGif.width()/2), explosion.getY()-(explodeGif.height()/2)); } } ... } 
+5
source

All Articles