Jesse
Jesse

Reputation: 599

Dealing with different screens

As a natural part of writing a real-time game for Android, I'll need to be able to support various screen resolutions, densities, etc.

From the primary article in the Android SDK Documentation, I'm aware that I should create separate resource for each bitmap in my game, to accommodate different screen densities; but I don't quite understand what to do about positions, velocities, etc.

The document says that I should use density-independent units in my code wherever I use explicit values. But doesn't that mean I would need to convert absolute pixels to density-independent units every single time that I use an explicit value?

For instance, I am developing a game (in landscape) using a high-density 800x480 display. Now, I am comfortable with moving a sprite at 1 pixel per frame (after delta time, etc). So that would look like:

sprite.setVelocityX(1);

If I were to use density-independent units, like the documentation suggests, wouldn't I have to do something like this everywhere that I use an explicit number?

sprite.setVelocityX(convert(1));

public float convert(float value)
{
    return value * getResources().getDisplayMetrics().density;
}

It feels like I'm not understanding this correctly...

Upvotes: 0

Views: 202

Answers (1)

Aleadam
Aleadam

Reputation: 40391

I have no experience in animation, but the first idea that comes to my head is to define a unit as a

static int UNIT = getResources().getDisplayMetrics().density;

and then use it as

sprite.setVelocityX(3*UNIT); // replaces sprite.setVelocityX(3);

That way you create an int once and avoid querying DisplayMetrics every time.

That said, there might be OpenGL or AndEngine mechanisms to get density-independent values directly.

Upvotes: 1

Related Questions