0

I have read all the pages I could find about support multiple screens in android, including

Supporting Multiple Screens

Providing Resources

Screen Sizes and Densities

And many others. But I still don't understand what resources I should provide for it to correctly position drawables(sprites) on a Canvas.

It's my first time making a game and I am currently a game Whack a Mole. I am confused about how to use the ldpi, mdpi, and hdpi, folders and how to properly position sprites to draw over a SurfaceView canvas.

I have a background image, resolution 480x800, so I added it to my hdpi folder. Then I have 120x150 sprites of moles, that I should position correctly on the holes for that background.

Currently I am using the following code to draw it:

canvas.drawBitmap(toDrawBitmap, draw_x, draw_y, null);

draw_x and draw_y are pixels that I found trying to place them correctly:

So far everything is fine, they are correctly placed in my hdpi, 480x800 screen. And android re scales them correctly on the different resolutions.

But when I try to use a different resolution screen, they are all drawn in wrong places, out of the holes, some of them are even out of the screen.

Please correct me if I am wrong but for what I've read these three resolutions are the most common in phones:

240x320 small - ldpi

320x480 normal - mdpi

480x800 normal - hdpi

My goal is to make the game work properly in those three kinds of screen. So my question is:

Can I calculate a draw_x and draw_y value, that will work on all of the devices? If not, how do i solve this problem?

caiocpricci2
  • 7,714
  • 10
  • 56
  • 88

1 Answers1

0

Yes you can calculate it using device width and height.

final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

final int width = display.getWidth();
final int height = display.getHeight();
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68