I want to animate a character (for eg run a dog) on screen. AnimationDrawable seemed a perfect fit for this, and AnimationDrawable requires an ImageView. How do I add and move around the ImageView in SurfaceView?
Thanks.
I want to animate a character (for eg run a dog) on screen. AnimationDrawable seemed a perfect fit for this, and AnimationDrawable requires an ImageView. How do I add and move around the ImageView in SurfaceView?
Thanks.
You don't need an
ImageView
.If your animation is an
XML Drawable
, you can load it directly from theResources
into anAnimationDrawable
variable:Then set it's bounds and draw on the canvas:
You also need to manually set the animation to run on it's next scheduled interval. This can be accomplished by creating a new Callback using
animation.setCallback
, then instantiating anandroid.os.Handler
and using thehandler.postAtTime
method to add the next animation frame to the currentThread
's message queue.With a SurfaceView it is your responsibility to draw everything within it. You do not need AnimationDrawable or any view to render your character on it. Take a look onto Lunar Lander example game from Google.