All about Android |

Mrz/11

15

Android 3D Development with jPCT

I spent a lot of time to find a good 3d framework for my next project.
On of my favorites is jPCT. jPCT is a fast and easy to learn 3D engine for Java and Android. It offers support for software and hardware rendering.

If you are interested, following links could be interested for you:
Hello World sample
jPCT for Android – Download

No tags

Jan/11

21

How to use Custom Fonts

The preinstalled fonts on Android are not really cool.
If you want use your own created or other free fonts you can use the following code snippet to embed them:

First of all, create in the res folder a new folder called “fonts”.
Copy your font file in the fonts directory.

To handling fonts you need a Typeface object:


private Typeface yourFont;
yourFont = Typeface.createFromAsset(getContext().getAssets(),"fonts/yourfile.ttf");

If you want use the font inside an Canvas as an Paint object you have to do the following:


Paint dummyText = new Paint();
dummyText.setTypeface(yourFont);

The following code snippet helps you if you want use the font in your normal XML Layout:


TextView textElement = (TextView) findViewById(R.id.yourElement);
textElement.setTypeface(yourFont);

Visit this Google Website for free fonts.

No tags

I dont know why i got this problem because i didn’t touched any settings.
My solution for this mysterious bug is the following:

- Start Eclipse and select your Project
- Project -> Properties -> Java Build Path -> Libraries
- Remove the Android library like “Android 1.6″, “Android 2.1″ and so on.
- Now, clean your project: Project -> Clean -> Clean projects selected below -> select your project and click the button “OK”.
- Add the Android library again and make a refresh (F5).
I hope that helps.

· · ·

Jan/11

3

Android OpenGL ES Tutorials

For all Open GL ES beginners, here are good tutorials for you:
The best general OpenGL ES tutorial that helps me a lot with 16 lessons:
http://insanitydesign.com/wp/projects/nehe-android-ports/

Also a great resource for the basic OpenGL ES knowledge (6 parts):
http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html

Further tutorials:
http://www.anddev.org/..
http://blog.jayway.com/…

For further informations, the official OpenGL ES documentation:
http://www.khronos.org/opengles/

Hope, that helps you to develope awesome games for the Android platform. ;)

· ·

It is a fact of using a language like Java that your binaries are easily decompiled.
It isn’t possible to avoid decompiling, but you can disguise your sourcecode.

What you need is a code obfuscator.

Obfuscators are basicaly replaces meaningfull class, variable and method names with random meaningless names.
This process makes it harder to understand the decompiled code for attackers.

Short sample:

Decompiled source without Code Obfuscator:

int number0 = 4;
int number1 = 1850;
int output = 0;

private void calc(){
output = number0*number1;
}

Obfuscated and then decompiled source

int xyk5 = 50/10-1;
int un82a= 5550/3;
int cbjes= 0*19;

private void uhmem(){
cbjes = xyk5 * un82a;
}

But Obfuscated code is harder to debug. For example stack traces are also encrypted. You need conversion tables to translate stack traces into their original names.

A good opensource Java Obfuscator with Android support is ProGuard.

Thanks for reading.
Andy

No tags

If you want create an app that should run on all android devices, it is important to handle all the different screen sizes.

1. The Density-independent pixel

A short sample:

We create in our layout a button:

<Button android:id=”@+id/testButton”
android:layout_width=”100px”
android:layout_height=”25px”>

The “testButton” has a height of 25 pixels and a width of 100 pixels.
What would happen if we run this test application on different android devices with different screen solutions?

In the first picture (Low density 120dpi) our button appears bigger, because we have lesser pixels at the same screen area.

The Button in the second picture (Medium density 160 dpi) appears smaller, because the pixel density is higher.

With a high density display the button is 33% smaller than the previus button with 160 dpi. Because there are more pixels at the same screen area.

How can we fix this problem?

The solution is easy:

We must only use the dip (Density-independent pixel) instead of px(Pixel).

The dip is a virtual pixel unit that android application can use to defining a similar User Interface.

The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen or 1 density-indepent pixel is equivalent to 1.5 physical pixel on a 240 dpi.

<Button android:id=”@+id/testButton”
android:layout_width=”100dip”
android:layout_height=”25dip”>

2. The layout

For a perfect Layout you should use the RelativeLayout instead of AbsoluteLayout.

With an AbsoluteLayout you define an exact position for each element. So, it is impossible to handle a similar UI for different screen sizes.

Therefore, you should use a RelativeLayout to design a uniform User Interface.

No tags

Aug/10

12

Canvas – How to render large Bitmaps?

My strategy game uses a large background image ( > 1000px x 1000px).
Canvas must render the full texture each frame! But a mobile phone can not display the full texture. So, that methode costs a lot of cpu time. Thats horrible!

The solution for this problem is easy:
Why should we render parts of the background image that a user can not see!

My solution:

//scrollingPositionX == Position on Map X
//scrollingPositionY == Position on Map Y
//canvas.getWidth()  == Width of the viewport
//canvas.getHeight() == Height of the Viewport

Rect rect1 = new Rect(scrollingPositionX, scrollingPositionY, scrollingPositionX + canvas.getWidth(), scrollingPositionY + canvas.getHeight());
Rect rect2 = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());

canvas.drawBitmap(largeBitmapTexture, rect1, rect2, new Paint());

First, we create a rectangle with the part of the bitmap which we want to display.
Than we create a second rectangle with the viewport dimension.
Last, the Canvas methode drawBitmap can handle this two Rectangle and we have a very performant way to render large Bitmap.

Thats simple but very effective, because we render only visible pixels of the bitmap!

No tags

Jul/10

17

Java performance tips

I am working on an strategy game for the android platform and therefore i want tell you some performance tipps.

This tips should you help to make your games a little bit smoother.

First rule
Performance optimisations should not never do at the end of a coding project. A high performance is very important and should get a high priority from the outset.

Threads
Your main loop will need to run on its own thread.  Android has a main UI thread and if you don’t run your own thread, the UI thread will be blocked by your game which will cause the Android OS to not be able to handle any of its normal update tasks

Use benchmarks
You should always measure performance and use the Log class to evaluate the benchmarks results.

Correct loops
Two possible loops:

public void first() {
for (int i = 0; i < yourArray.length; ++i) {
// FIRST LOOP – CODE
}
}

public void second() {
int len = yourArray.length;
for (int i = 0; i < len; ++i) {
//SECOND LOOP – Code
}
}

First loop is slowest, because  the cost is to expensive of getting the array length once for every iteration through the loop.

Second loop is faster, because with this methode we are avoiding the lookups (Array.length).

Avoid Getters and Setters
Virtual method calls are expensive and costs cpu time, much more so than simple instance field lookups. It looks dirty but its faster (3x – 7x)!

Object creation
Avoid unnecessary object creation and therefore do not initalize local variables. Make all variables and objects public. That has the result that the garbage collection must never run. The runtime of the garbage collection costs 150-400ms! It will also pause all other threads and would run every 4 – 7 seconds.

General tips
Declare variables final if they are not modified after being initialized.
‘==’ is faster than equals().
Use StringBuffer instead of +.
Make classes final.
Avoid unused code inside loops and use “breaks”.
switch is faster than a complex if-else construction.
Avoid loading (import) classes you do not need.

Important:
You should keep clearly in mind that these tips helps nothing when you are using a bad design!

Thanks for reading.

Andy

No tags

Jun/10

24

13 facts why Android is better than iOS!

I thought its time to write down some facts why Android is better than iOS!

My advice, should you be an Apple Fanboy, Apple Lover,  Apple fanatic or Steve Jobs, I would you not recommend to read further. Because you dont want to read it.  But should you have interests in other mobile platforms such as Symbian, WebOS, Windows Mobile User or Android, then you should read further.

WHY ANDROID IS BETTER THAN IOS4

1st Android is open and free
The new iOs4 version is not compatible with the first generation of iPhones, and does not appear on older Apple products! That is a fact. Android is open and a great Android OS developer community is active and works hard to make Android better. This great community is the reason why Froyo will be released  on the oldest Android devices. There is no fragmentation on the Android platform!

2nd Its more open
There was an app in the app store. This app was a little “home screen” app with some additional features than the standard homescreen. But Apple removed this app from the market. Apple does not want alternative homescreens. In the Android Market there are a lot of different home screens. Manufacturer like HTC and Sony Ericsson sells their Android device with his own surface. You dont like the surface? Thats not a problem! You can easily install other surfaces.

3rd Multitasking
Shortly, there was a report that tests the implementation of multi-tasking in different mobile os platforms.

The solution is obvious. WebOS has the best implementation of multi-tasking. The second place got Android. iOS got the last place.
The reasons are simple, because the multitasking implementation in iOS is very restricted. Some people calls the multitasking feature in iOS also fake-tasking.

4th Google Integration
All Google products are pre-installed. For example: Gmail, Talk, Maps, Goggles, calendar, search and Youtube.
You can of course replace the google products through alternative apps that you can download in the android market.

5th Usability
Android offers more options for personalization.
Short sample:
You can  move Widgets on the homescreen and folders can be created. You could also change: wallpapers(live wallpaper), boot screens, lockscreen, themes and so on. You have full access on the filesystem and you can create links.

6th Choose your hardware
HTC, Motorola, Sony Ericsson, Samsung, LG sells Android phones. With this great range of hardware you can find your personal android phone with your budget and hardware requirements.

7th Flash Support
Android 2.2 supports official Flash 10.1.  Steve Jobs told us that Flash will never appear on iOS.

8th App Market
The App Store from Apple has more apps than the Android Market. But whats the meaning of the developer about this two stores? Apps for the App Store will only published if Apple says “YES”. Apple investigates if an app corresponds all the Apple guidelines.

Even the layout: Apps, Button color and so on are prescribed. Android App developers have significantly better. In other words, there are no guidelines for android apps.

9th Synchronization
Google phones come without a synchronization software such as iTunes.
There is also a support for microsoft outlook, thunderbird and so on.

10th Extended memory
Almost all Android smartphone have memory card readers. iPhone fans wait in vain for.

11th Replaceable battery
In all Android devices, you can easily change the battery without any problems.
The iPhone must send to the support. That costs additional money.

12th Updates
If you want to update the iPhone OS you must know, that you must connect it to a PC to start the update process.
Android updates comes via the Internet (OTA, Over The Air) directly on the Android phone.

13th Warranty
The most iPhone User do at first jailbreak. Because whitout jailbreak they could not do anything ;)
But with this step they will loss the warranty. Android Users do not know this problem.

Yesterday, I found this amusing video :)

Have fun…

Thanks for reading.
Andy

No tags

This is the third article of my little blog serie “How does android work?”.
Now, in this article i want to explain the operation of the memory management in Android.

First of all, many people say “Why can i not close an app? It does need memory and its very unperformant”.
Yes if the person is a windows user the statement would be true. The more apps are running the merrier lesser memory is free.
In android the memory management does automatical kill “old” processes to reclaim resources.

If you close an application, the application will switch in the background (Idle) mode. The state of the current running activity will be saved to the system process.
Then you start some more apps the memory managament detects automatical if the free memory is to “less” and terminates old process automatical. Thats the reason why Android doesnt need a swap file and users must not close applications.
Throughout the termination of the app process we get more memory resources and can start the new activity (process).
Android keeps a LRU (last recently used) list and starts killing the oldest unneeded process if memory needed.

An another effect of the LRU is, that the user can go backwards. For sample: You read a mail and click a link. The browser will be opened. From the Browser there is a videolink for youtube. The youtube app starts. Then the youtube video is over you can go back with the backward buttons and you are realy fast in your mail application back.

The advantage is, that the “closed apps” while they are running in background doesnt need any cpu time and also so no battery power. But yes, they need memory resources.

Android OS Developer could be interested the “lowmemorykiller.c” file in the directory “drivers/misc“. In the kernel source file you could set up exact memory rules for the memory manager.

If you are know interested and want to see your current memory setting, take a look there: /sys/module/lowmemorykiller/parameters/minfree

On my HTC Hero the settings are: 1536,2048,4096,5120,5632,6144

These 6 values (unit pages) are memory limits for different kind of applications when the memory management starts killing processes.

The advantage to change memory setting directly in kernel is that you don t need a special task killer app which must run allways in background!

If you search for “Android and Memory” there are a lot of different meanings about the possible truth of memory management in android.

If you have any questions or want tell me anything please leave a comment.

Thanks for reading.

Andy

No tags

Older posts >>