Question

I've got a problem when supporting different Devices and their screens: I have game where I draw a lot of 70px*70px icons in a grid.

The .png File is 70*70 @ 315ppi.

In my java code I now draw the images to a grid with the following code:

for (int x = 0; x < Map.getxSize(); x++) {
        for (int y = 0; y < Map.getySize(); y++) {
            ballSprite.setX(x*70);
            ballSprite.setY(y*70);
            ballSprite.setCurrentFrame(Map.mArray[x][y]-1); //-1 because 0 field is empty
            ballSprite.onDraw(canvas);
        }
    }

the (x*70) works great with my Galaxy Nexus, but when I test it on a device with hdpi 800 the 70px value is way too high.

What is the best way to adapt this value to the different screens? Thanks for helping me out!

Was it helpful?

Solution

Multiply X and Y with proper scale factors.

Display display = getWindowManager().getDefaultDisplay(); 
int currentWidth = display.getWidth();  
int currentHeight = display.getHeight();

float scaleX = width that application was written for (in your case Galaxy Nexus width) / currentWidth  .
float scaleY = height that application was written for (in your case Galaxy Nexus height) / currentHeight .

ballSprite.setX(x*70*scaleX) etc...

OTHER TIPS

Please refer yourself to

 https://developer.android.com/training/multiscreen/screendensities

And you'll see

36x36 (0.75x) for low-density (ldpi)
48x48 (1.0x baseline) for medium-density (mdpi)
72x72 (1.5x) for high-density (hdpi)
96x96 (2.0x) for extra-high-density (xhdpi)
144x144 (3.0x) for extra-extra-high-density (xxhdpi)
192x192 (4.0x) for extra-extra-extra-high-density (xxxhdpi)

these you'll see 0.75x to 4.0x am pretty certain you'll use these values to scale the UI objects for screen density. You might find more but these are the main ones.

PS new devices don't use the from mdpi to xxxhdpi. This you'll see in the link.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top