Android Development

Tag: graphic

How to: Create a scrollable Map with Cells

by Martin on Jan.04, 2010, under how to, tutorial

This how to will show you how you can create a simple 2D Map with Cells to place stuff on it. Just like the old school SimCity.

The first thing you need is an Activity with a SurfaceView and a Thread to trigger the drawing. Who doesn’t know these fundamentals, please read my series on 2d graphics first.

Lets start with the smallest unit for our map: the Cell.
Each Cell will have a background color and a unique ID.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.droidnova.android.games;
 
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
 
/**
 * A part of the map.
 */
public class Cell {
    public int _id = 0;
    public int _backgroundColor = Color.GREEN;
 
    /**
     * Konstruktor.
     * @param id
     */
    public Cell(int id) {
        _id = id;
    }
 
    /**
     * Draw the cell
     *  
     * @param canvas Canvas to draw on.
     * @param paint Color of the "pencil".
     * @param x X coordinate.
     * @param y Y coordinate.
     */
    public void draw(Canvas canvas, Paint paint, int x, int y) {
        paint.setColor(_backgroundColor);
        canvas.drawRect(x, y, x + CellMap._cellSize, y + CellMap._cellSize, paint);
 
        paint.setColor(Color.BLACK);
        canvas.drawText("" + _id, x + 1, y + 10, paint);
    }
}

On line 32 you see, how we draw the cell. The variable _cellSize is a static variable from CellMap, which will be introduced later. Everything else should be already known.
(continue reading…)

  • Share/Bookmark
10 Comments :, , , , , more...

Creating Game Menus in Android

by Stephen Flockton on Sep.30, 2009, under how to, tutorial

As requested here is a sample tutorial in how to create a menu system for games in Android. Sorry for the delay but this tutorials take time to code test and write up. Anyway I hope you find it useful.

Before I jump into the code I’m going to take a second to explain my way of coding menus in Android. As we all know Android is built on the concept of activities. If you have been following earlier tutorials you already know how to create activities which can display graphics and deal with player input. But what if you want several different screens, such as options or credits? You could code them all into one activity but you would end up with a bloated and hard to maintain class.
(continue reading…)

  • Share/Bookmark
14 Comments :, , , , more...

2D Sprite Animation in Android Addendum

by Stephen Flockton on Sep.23, 2009, under how to, tutorial

If you read my last tutorial entry here, then you may have come across a problem with the code.

If you load several large bitmaps using the BitmapFactory class to decode the bitmap you application will give you the dreaded force close dialogue box. A quick look in the logcat shows that a bitmap exceeds the virtual machine memory budget.

1
ERROR/AndroidRuntime(750): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget

You may wonder how its possible that a few PNG’s can take up all the 16 mb of memory allocation for an application.
(continue reading…)

  • Share/Bookmark
5 Comments :, , , , , more...

2D Sprite Animation in Android

by Stephen Flockton on Sep.13, 2009, under tutorial

In order to make anything more than very simple games in 2D it’s important to be able to draw animated sprites.

There are several ways to create animated sprites in Android, including using XML based declarations and swapping between multiple bitmaps. But when I came to write my animation code neither of these methods was flexible enough for the kind of animation I needed.

Instead of using these methods I instead used sprite sheets and the functionality provided by the Android bitmap drawing functions to draw each frame of the animation. In a nutshell each frame of animation is set on a single bitmap one after the other. The animation code then plays one frame after the other until the animation is complete. Below is an example of a sprite of a bouncing color changing arrow.
(continue reading…)

  • Share/Bookmark
25 Comments :, , , more...

Android 3D game tutorial – Part VI

by Martin on Sep.02, 2009, under tutorial

Updated to be Android 2.0.1 compatible.

You are new to this series? Please start with the first part.

The sixth part of this series will show you how you create the correct perspective because 3D is nothing without the correct perspective.

Before we start we should discuss the two possible “views” OpenGL offers: orthographic and perspective.
(continue reading…)

  • Share/Bookmark
28 Comments :, , , , , more...

Android 3D game tutorial – Part V

by Martin on Aug.21, 2009, under tutorial

Updated to be Android 2.0.1 compatible.

You are new to this series? Please start with the first part.

The fifth part of this series will show you how you can create your first full 3d object. In this case a 4 sided pyramid.

Some preparation will be needed to make our future development much easier.
We have to be more dynamic in calculating our buffers and creating arrays with the correct size. (continue reading…)

  • Share/Bookmark
8 Comments :, , , , , more...

Android 3D game tutorial – Part IV

by Martin on Aug.19, 2009, under tutorial

Updated to be Android 2.0.1 compatible.

You are new to this series? Please start with the first part.

The fourth part of this series will show you how to add some colors to you triangle.

In the last part we created a second static triangle to prove the rotation of the triangle and not the entire scene. We will now get rid of this static triangle by removing the function initStaticTriangle(), removing both buffers, _indexBufferStatic and _vertexBufferStatic, used for it. We also have to remove the last 4 lines of code of our onDrawFrame() where we initialized the static triangle. (continue reading…)

  • Share/Bookmark
4 Comments :, , , , , more...

Android 3D game tutorial – Part III

by Martin on Aug.18, 2009, under tutorial

Updated to be Android 2.0.1 compatible.

You are new to this series? Please start with the first part.

The third part of this series will show you how to stop the rotation of the triangle and that the rotation really just work on the triangle and not the “camera”.

We want to have more control over the rotation. To get that, we reset the matrix on every call of the onDrawFrame() method. This will reset the angle of our triangle so it always it stays rotated at the given angle on initialization. (continue reading…)

  • Share/Bookmark
7 Comments :, , , , , more...

Android 3D game tutorial – Part II

by Martin on Aug.17, 2009, under tutorial

Updated to be Android 2.0.1 compatible.

You are new to this series? Please start with the first part.

The second part of this series will show you how to add a triangle and how to rotate it a bit.

The first thing we have to do is to initialize the triangle we want to display. We have to create a function named initTriangle() in our VortexRenderer class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// new object variables we need
// a raw buffer to hold indices
private ShortBuffer _indexBuffer;
 
// a raw buffer to hold the vertices
private FloatBuffer _vertexBuffer;
 
private short[] _indicesArray = {0, 1, 2};
private int _nrOfVertices = 3;
 
// code snipped
 
private void initTriangle() {
    // float has 4 bytes
    ByteBuffer vbb = ByteBuffer.allocateDirect(_nrOfVertices * 3 * 4);
    vbb.order(ByteOrder.nativeOrder());
    _vertexBuffer = vbb.asFloatBuffer();
 
    // short has 2 bytes
    ByteBuffer ibb = ByteBuffer.allocateDirect(_nrOfVertices * 2);
    ibb.order(ByteOrder.nativeOrder());
    _indexBuffer = ibb.asShortBuffer();
 
    float[] coords = {
        -0.5f, -0.5f, 0f, // (x1, y1, z1)
        0.5f, -0.5f, 0f, // (x2, y2, z2)
        0f, 0.5f, 0f // (x3, y3, z3)
    };
 
    _vertexBuffer.put(coords);
    _indexBuffer.put(_indicesArray);
 
    _vertexBuffer.position(0);
    _indexBuffer.position(0);
}

(continue reading…)

  • Share/Bookmark
19 Comments :, , , , , more...

Android 3D game tutorial – Part I

by Martin on Aug.10, 2009, under tutorial

Updated to be Android 2.0.1 compatible.

The first part of this series will give you a short introduction to the OpenGL terminology and the first step in your 3D programming.

The series itself will be about a 3D game called Vortex.
The tutorial will focus on 3D programming, stuff like menu or life cycle may be part of the code but will not be introduced.

Lets start with the terminology of OpenGL.
(continue reading…)

  • Share/Bookmark
25 Comments :, , , , , more...