How to make an animation in an Android chess game

I am developing a chess game for Android ( androidchess.appspot.com ). If I want to add animation, should I use a custom view that extends Canvas (I'm doing it now), or a custom view that extends SurfaceView?

+2
android animation
Mar 13
source share
1 answer

I have not tried using View to extend the Canvas , but for my game I use the same method as the LunarLander example game:

 public class CustomView extends SurfaceView implements SurfaceHolder.Callback 

The usefulness of this is that it gives you handles for a SurfaceHolder (so you can call up the canvas that is drawn on the screen), and callbacks for surfaceCreated , surfaceChanged and surfaceDestroyed . This allows you to do things like drawing custom animations as soon as the surface is available, or make sure you are not trying to draw on the canvas after it has been deactivated. Looking through LunarLander, you should show how to use them correctly.




Edit: I recalled another reason why using SurfaceHolder was useful. This is due to the fact that, as I mentioned above, it allows you to directly access the canvas that is drawn on the screen. With SurfaceHolder, this is not done by overriding onDraw, but with something like Canvas canvas = mSurfaceHolder.lockCanvas() . (See LunarLander for the exact syntax.) The reason is that it is important because it allows you to precisely control when the drawing is taking place. If you can only work by overriding onDraw (), then the drawing will not happen until your program reaches the waiting phase. In other words, you cannot use invalidate() and onDraw() in a loop because drawing will not happen until the loop completes. And since you are likely to use loops for things like drawing a slice moving around the screen, this becomes a problem.

Note. You can avoid this problem by using multiple threads. I just have not tried it, as this is not required for my game; the only animation is a fixed-length animation in response to user input, and not something continuously moving in the background, so I have not experimented with multiple threads yet.

+3
Mar 13 '10 at 14:58
source share



All Articles