Monday, April 4, 2011

Beginning with Android 2D Graphics

I am planning to develop a simple Android game with the main intention of learning Android game programming while doing so. I plan to start out with a rather simple board game. The user controls a ball and has to direct it to a specific position (the target) in a maze. On reaching the target the game is complete and points will be awarded to the user based on the time taken to reach the target (i.e. lesser time higher score).

This post is intended as a reference guide (mostly for myself but if it does help anyone else that would be cool).

To get started, I first looked up options Android offers and found several ways of implementing 2D graphics. I had to dig a little deeper to know which was best suited for the task I had in mind.

The first option Android offers is drawing into a View object (with the android.graphics.drawable and the android.view.animation packages). However (from the developers guide) this option seems to be best suited for display of static graphics OR pre-defined animation within an otherwise static application. As this does not suit my needs I decided to check out the other options.

The next option is drawing to a Canvas, which from the developer guide is a better choice when the application needs to regularly re-draw itself (as in my case).
There are however 2 ways to go about this. Either have the game draw to the Canvas in the same thread as the UI Activity or handle this in a separate thread. Doing this in the same thread is the recommended approach for games that do not require a significant amount of processing or frame-rate speed (such as board games or any slowly animated game), while using a separate thread for updating the drawing surface is recommended for faster animations and gives the game/app more control on the drawing pace.

To summarize these are the options,
  1. Drawing to a View object - Suited for Display of Static graphics / pre-defined animations
  2. Drawing to a Canvas - For apps that need to re-draw regularly
    • In the same thread as the UI activity
    • In a separate thread from UI activity (more control on drawing pace)
With my first attempt I've decided to try drawing in the same thread, and if performance does not seem satisfactory, I will try the separate thread approach (or may as well try it anyway once done with this:)). In my next post I'll share whatever I've learned trying to implement this. The Snake game is given as a references for this kind of an app.

Updated: Continued here

No comments: