Tuesday, December 15, 2009

Adding Motorola Droid support for my Android games

A couple of weeks ago, a friend bought a Motorola Droid and I found that my Android games did not layout properly due to the higher resolution. The paddings and margins were too small, and fonts were WAY too small. I thought this was going to be a major headache...but it turns out it was an easy fix.

I just needed to change all my units from "px" to "dp" which stands for "density-independent pixels." This was easy to do for the layout XML files, but it was a little different for my code calculated sizes. For images that were based on the percent of the screen size, no change was necessary. However, I had some font sizes that were defined with a size of 16 pixels. The fix? Multiply that value by the value in DisplayMetrics.density()

http://developer.android.com/reference/android/util/DisplayMetrics.html

The density value is 1 for the resolution seen in the HTC G1. For smaller resolutions, you get a scaled value less than 1, and for higher resolutions, you get a value greater than 1. After making these changes to support the high resolution Droid....the game worked perfectly on a low resolution screen (i.e. HTC Tattoo).

I hope this helps others in adding support for high resolution screens to their applications.

1 comment:

  1. Hey, um, could you remove the protected mode or whatever it is for Dr. Droid that's keeping me from buying it?

    It's not really stopping any piracy, just getting in the way of legit people like myself who'd like to buy it but can't for whatever reason. In my case I think it's because I'm currently using my Galaxy Spica without a SIM and just using it with WiFi until my current cel contract is up.

    I hope Google sorts this out soon, but for now, it would be nice if developers wouldn't lock people out from being able to purchase.

    ReplyDelete