Android: Using OpenGL for 2D rendering – the easy way

This is an extension to the project developed by Chris Pruett, which can be found here [1]. Feel free to read the README provided.


Basically, what this project aims to do is to offer you a simple class that can be used to render 2D objects in an OpenGL surface.

I modified a little the project written by Chris Pruett by adding a wrapper class corresponding to a scene. This allows you to use OpenGL very easily in order to render 2D objects. You can instantiate this class, add various sprites and have them drawn on the surface view.

The project can be downloaded here [2].

This is the API for the Scene class:

public interface Scene {
	public SurfaceView getSurfaceView();

	 * Sets a bitmap as the background
	 * @param bitmap
	public void setBitmapBackground(Bitmap bitmap);

	 * Sets a nine patch resource as the background. It will stretch to fill the
	 * entire screen. When screen orientation will change, this background will
	 * also change to fill the new screen.
	 * @param pictureId
	public void setNinePatchBackground(int pictureId);

	 * Sets the background color of the scene. No bitmap will be used any more
	 * for the background.
	 * @param color
	public void setBackgroundColor(CustomColor color);

	 * Adds a sprite to the scene.
	 * @param sprite
	 * @param addToMover
	 *            Whether to pass this to the Mover thread
	public void addSprite(GLSprite sprite, boolean addToMover);

	 * Sets the mover thread. This will handle the sprites' movement.
	 * @param mover
	public void setMover(Mover mover);

	 * Creates a sprite corresponding to the given resource id
	 * @param context
	 * @param resourceId
	 * @return
	public Renderable createSprite(Context context, int resourceId);

	 * Returns a sprite corresponding to a given bitmap
	 * @param bitmap
	 *            The bitmap used for the sprite
	 * @param bitmapId
	 *            The bitmap id is used to re-use textures (use same ID for same
	 *            bitmaps content)
	 * @return
	public Renderable createSprite(Bitmap bitmap, int bitmapId);



public class MainActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {

		Scene scene = createScene();

	private Scene createScene() {
		// our OpenGL scene
		GLScene scene = new GLScene(this);

		// can set a nine patch for the background

		// can set a bitmap for the background
		// scene.setBitmapBackground(someBitmap);

		// can set a color for the background
		// scene.setBackgroundColor(new CustomColor(0, 0, 0, 1));

		// create some sprites
		GLSprite sprite = scene.createSprite(getApplicationContext(), R.drawable.skate1);
		// initial position
		sprite.x = 100;
		sprite.y = 200;
		GLSprite sprite2 = scene.createSprite(getApplicationContext(), R.drawable.skate2);
		// initial position
		sprite2.x = 300;
		sprite2.y = 400;

		// add sprites to the scene
		scene.addSprite(sprite, true);
		scene.addSprite(sprite2, true);
		scene.addSprite(sprite.clone(), true);
		scene.addSprite(sprite2.clone(), true);

		// this thread controls the sprites' positions
		scene.setMover(new Mover());
		return scene;

GL 2D scene preview



3 thoughts on “Android: Using OpenGL for 2D rendering – the easy way

Add yours

  1. Hi,

    Does this use traditional x-y screen coordinates or the flipped coordinates of OpenGL.

    Have been looking for a simple 2D openGL implementation for a while now, so this seems quite interesting.


    Michael a.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

Up ↑

%d bloggers like this: