Android Development

Rapid android development from Berlin

Browsing Posts tagged sources

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

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

Every new Android SDK Version means that someone has to collect the sources and bundle them. Thanks to the supporter of the missing source jar issue a new source jar is provided. Also please star the issue to flag that issue as important!

Install instructions:

To use it in Eclipse, create a directory sources/ inside your
/platforms/android-1.6/ directory, and unzip the archive there. Start
Eclipse and check that you can see the source, for example ask for the definition of
the Gesture class (new in 1.6), you should see the source code, not the decompiled
byte code.

We will mirror the sources to, so feel free to download and use it!
Android SDK 1.6 sources

Share

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

In our little sample application we used the strings.xml to define static texts.
If you want to use the build-in multi-language support you just have to follow some easy conventions.
To use localization for strings you have to add a new directory to res/ and name it “values-de” for the german language.
Create a strings.xml in this directory and deploy your application. If you now change your language on the device or emulator, the phone picks the strings.xml with the matching local code and uses it – in the same manner you could provide images etc. for a specific language (i.e. graphical buttons).
Works well for our little sample.

Our strings.xml for the german language:

1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Wähle einen Freund</string>
    <string name="button_info_text">Bitte wähle einen Kontakt aus</string>
    <string name="button_text">Auswahl</string>
    <string name="contacts">Kontakte</string>
    <string name="chosen">Sie haben ausgewählt:</string>
</resources>
Share

One of the annoyances anyone starting android development has to go through is getting hold of the android sdk sourcecode.

Surprisingly the sourcecode does not come bundled with the sdk, nor is there a way to get it seperately as an android-src.jar like with the sun sdks. To top it off, you actually can get the sources by google – but only using MacOS or *nix to check out from their repository.

Even with the recommended OS this is far from a trivial task but concerning developers using windows it’s unfriendly to say the least.
continue reading…

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