Android Development

Rapid android development from Berlin

Browsing Posts tagged tutorial

This tutorial series is build against Android 2.1 and should also work on newer versions. This version is still supported because of the amount of devices that still run this version. There is an earlier, but a bit outdated version of this part.

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

The fifth part shows you how to animate the bitmaps. That means you touch the screen and from there on the icons move around the screen. The direction of the movement and the speed itself will be random. The animation will be constant and independent of the frames per seconds (FPS). The FPS will be displayed on the left top corner of the screen so that everybody can see the changes in the performance over time.
continue reading…

Share

This tutorial series is build against Android 2.1 and should also work on newer versions. This version is still supported because of the amount of devices that still run this version. There is an earlier, but a bit outdated version of this part.

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

The fourth part will introduce how to add more bitmaps than the just the one. At the end you should be able to have dozens of bitmaps on the screen.

First of all we need to encapsulate the bitmap handling into a separate class. We will name the class Element. It will contains the coordinate and the bitmap and it will have its own drawing method.

The basic class looks like that and the content of the methods should be already familiar.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Element {
    private int mX;
    private int mY;
 
    private Bitmap mBitmap;
 
    public Element(Resources res, int x, int y) {
        mBitmap = BitmapFactory.decodeResource(res, R.drawable.icon);
        mX = x - mBitmap.getWidth() / 2;
        mY = y - mBitmap.getHeight() / 2;
    }
 
    public void doDraw(Canvas canvas) {
        canvas.drawBitmap(mBitmap, mX, mY, null);
    }
}

continue reading…

Share

This tutorial series is build against Android 2.1 and should also work on newer versions. This version is still supported because of the amount of devices that still run this version. There is an earlier, but a bit outdated version of this part.

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

The third part of the tutorial will introduce some interaction to our sample application. We will implement the possibility to display the bitmap at the touched position.

First of all we need some member variables to store where the touch happened. That will be mX and mY of type integer.

1
2
private int mX;
private int mY;

continue reading…

Share

This tutorial series is build against Android 2.1 and should also work on newer versions. This version is still supported because of the amount of devices that still run this version. There is an earlier, but a bit outdated version of this part.

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

The second part of this series will show what you have to change to switch from using the View class to SurfaceView class.
The advantage of switching the parent class is the easy possibility, to draw everything you want on the display without working with layouts and XML files. It is also the best way to do custom animations and, of course, games.

continue reading…

Share

This tutorial series is build against Android 2.1 and should also work on newer versions. This version is still supported because of the amount of devices that still run this version. There is an earlier, but a bit outdated version of this part.

The first part of this series will show you, how to display an image in a normal View.

First we create a new Project with the activity named Tutorial2D.
We will see this:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.droidnova.android.tutorial2d;
 
import android.app.Activity;
import android.os.Bundle;
 
public class Tutorial2D extends Activity {
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

This maybe looks familiar to you.
continue reading…

Share

This part is not a real part 3 but it is an enhanced implementation of Part II by Dave ( drbixler [at] gmail [dot] com ) who wrote this part as a guest author. Thank you Dave!

While reading the post, you should already look at the code so first download it: Cell Map Enhanced

I’ve been working on my first Android application for a few months now (evening work so it’s taking me a little longer than I had hoped). My game requires a 2D map of cells that can scroll in any direction. Having looked around for examples I stumbled across this series and went to work. I tend to be the type of person that “adopts, changes, and enhances” when I use code I find on the internet. I’m very particular about what my code looks like, how it’s commented, etc. What I ended up with was a reusable 2D map scrolling framework that could smoothly scroll in any direction and also supported “fling” scrolling. I’ve also put in the basics of zooming, though didn’t take the plunge into pinch zoom. Again, I completely redid a lot of the code from the 2nd part of this series but there are still some similarities.
I’m going to only really focus on one or two specific classes because most of the code provided is very short and self-explanatory. I make it a point to comment verbosely so that should help as well.
continue reading…

Share

Sorry about the delay but I have finally managed to find the time to complete the Sound effect Tutorial which I hope you all enjoy.If you have not already check out the first tutorial here before reading this one, as it reuses a lot of the code.

In the previous tutorial we had a basic working sound system, but it was limited in scope. it could only be used in the class it was declared in and could not be used between activity’s easily. Ideally we would like one instance of the Sound Manager that could be used across the entire app life-cycle.

The solution is the Singleton design pattern explained better here.In essence, we will create only one instance of the Sound Manager class that can be accessed anywhere within the application.
continue reading…

Share

The second part of this series will show you how you can scroll smoothly over the simple 2D Map which was created in the first part.

Note: I changed my coding style to fit the Java/Android coding style. Please be aware that variables like _mapSize are now mMapSize.

The performance issue we discovered in the first part was awful and no one will play a game which needs seconds to draw another frame. But why do we have this performance issue?
Do you remember how we draw the Map? We go trough the map in a loop and draw each cell. If our map has only a size of 10, everything is fine, but if we go to 100 and more, we draw a lot of cells and most of them are not on our display. And thats the mistake: We use resources and time to draw cells we don’t see.
continue reading…

Share

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

In today’s tutorial I am going to show you my method of creating, managing and using sound effects in Android. In this first part I’ll show you the basic method of encapsulating your sound management code. This method works best when you have a typical application, or simple game all in one thread. After you have read this check out Part 2 which will show you a more advanced way to manage your sound across multiple classes.

The method I use to play sounds is to use the Sound Pool classes rather then the Media Player classes that the Android dev-guide seems to suggest. While there is nothing wrong with using the Media Player classes for simple applications they did not provide the flexibility I needed.
continue reading…

Share
Powered by WordPress Web Design by SRS Solutions © 2012 Android Development Design by SRS Solutions