My app implements its own sprites by calling the following in my view's onDraw()
method:
canvas.drawBitmap(sprite.getBitmap(), sprite.getX(), sprite.getY(), null);
The app is a physics simulation, and so far this has worked out great. But now I'd like to enhance the animation by morphing between images for the sprites when certain events occur.
For example- when a collision happens, I would like to play an animation of an explosion. My idea was to replace the usual sprite bitmap with that of an explosion PNG, and use Android "tween animation" to make the explosion grow larger.
But the Android tween animation example assumes that you have an ImageView
defined somewhere statically in your XML configuration.
Is there a way to animate a bitmap as drawn in onDraw()
using tween animation? Or do I need to convert my sprites to use some sort of ImageView
? If the latter, can you point me to an example of proper sprite animation in Android?
Thanks
If you are creating a game, you can use Vectors(Mathematical) to update the position of your images and then draw then with getX and getY. You should iterate and update this vector in some direction through some speed.
Those Vectors are not native (Game APIs has).
You need have a loop to iterate and update the position, then redraw.
For games, it's not a good idea to have components such ImageViews to make animations. They need the system to calc all the layout again and again over time.
You could draw and animate your explosion in Adobe Flash, export the sprite images using swfSheet or similar and then use Frame animation
You can do the tween animation without the
ImageView
coming from an xml file, but it does need to actually be a View in your view hierarchy.The drawing you're doing in your
Canvas
is opaque to the view hierarchy. I think you have two options:ImageView
on top of your custom view and animate that using tween animations.Canvas
draw routines to animate your explosion.I'd say that using
Canvas
routines, along with their maxtrix transformations, makes sense given that you probably already have an animation thread in your app.I built a generic Tween Engine in java that you can use to animate anything, including your sprites. It's optimized for Android and games because it does not allocate anything at runtime, to avoid any garbage collection. Moreover, Tweens are pooled, so really: no garbage collection at all!
You can see a complete demo here as an android application, or here as a WebGL html page (requires Chrome)!
All you have to do is implement the
TweenAccessor
interface to add Tween support to all your sprites. You don't even have to change your Sprite class, just create aSpriteTweenAccessor
class that implementsTweenAccessor<Sprite>
, and register it to the engine at initialization. Just have a look at the GetStarted wiki page ;)http://code.google.com/p/java-universal-tween-engine/
I'm also building a visual timeline editor that can be embedded in any application. It will feature a timeline similar to the Flash authoring tool and Expression Blend (a Silverlight dev tool).
The whole engine is heavily documented (all public methods and classes have detailed javadoc), and the syntax is quite similar to Greensock's TweenMax/TweenLite engine that is used in the Flash world. Note that it supports every Robert Penner easing equation.
Of course, no tween engine would be complete without providing a way to build powerful sequences :)
I hope you're convinced :) There are a lot of people already using the engine in their games or for android UI animation.