views:

1024

answers:

3

I am currently starting a game engine in Android, first forray onto the platform and have the basics in place however i am unsure of the best way to approach resolution independence when using SurfaceView to draw graphics.

Looking for pointers as to how to keep the game / sprites etc all looking the same independent of the screen, obviously it wouldn't be efficient to scale all the sprites every frame or store many variations for differing resolutions

+1  A: 

There are two simple approaches:

First:

On creation of a Surface view, it calls:

onSizeChanged  (int w, int h, int oldw, int oldh)

Create a few global variables, x & y so if you override this with:

onSizeChanged(int w, int h, int oldw, int oldh){
this.w=w;
this.h=h;
}

Then in any drawing or game calculations you can use the window w and h.

Second:

call getWidth() & getHeight() in drawing and game calculations.

Good luck with your game!

Laurence Dawson
Thanks Laurence, i guess my question is more aimed at how to deal with the scaling if ingame bitmaps so that the game appears the same on all platforms...
Tom
+5  A: 

You'd just scale the sprites when you load them. I am guessing you're loading them as Bitmaps from a Resource, right? If that's the case then all you need to do it something like this:

BitmapFactory.Options options = new BitmapFactory.Options();
options.outHeight = spriteHeight * scale;
options.outWidth = spriteWidth * scale;

Bitmap sprite = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.sprite, options)

Where scale is based on the change of the screen size.

CaseyB
Cheers Casey, with this solution do you think it would be best to incorporate the highest possible sprites suitable for the larger android resolutions like the Nexus and then downscale the graphics for older devices?
Tom
I would think so, things tend to look better when you scale them down than they do when you scale them up.
CaseyB
So i tried this code, and have gone through the documentation.. for some reason the sprite is always output at the same size even if i hardcode outHeight and outWidth variables.
Tom
Ok, I did some researching and it turns out that outHeight and outWidth are outWidth are output values. You need to:set inDensity to the screen density that would be native for the bitmap.set inTargetDensity to the screen density of the device your app is running on.and set inScaled to true.Then you will be given a bitmap that is scaled for your screen and you can get the size from the outWidth and outHeight values.
CaseyB
A: 

I also need to do this and am confused about the inDensity and inTargetDensity parameters.

inTargetDensity = screen density of the current device running the application. I get how you can retrieve that using metrics.densityDpi.

However, what is inDensity? The density of the particular resource based on the folder it was in? How do you get the density of a resource?

Greg