0

I'm creating a game that is full screen. I'm using an image for the background and I'm drawing images on top of it. The background image looks great in all the devices, but I can't draw the overlay images in the same spot on all devices.

For example if I use

canvas.drawBitmap(overlay, 365, 85, paint);

It will align the image perfectly for phones that have 854px width. But anything lower then that it will be pushed way to the right.

I've tried playing around with dip but I can't seem to figure that out. Is there any where I can ensure that they items line up correctly on all resolutions?

Dave
  • 324
  • 1
  • 4
  • 10
  • You should pass the x , y co-ordinates according to the device screen size not a const value. – Sandy Jan 04 '11 at 12:29

2 Answers2

1

If you want to do it like this you probably need to caclulate the position based on the size of the screen.

You could find these using something like this:

   DisplayMetrics dm = new DisplayMetrics();
   getWindowManager().getDefaultDisplay().getMetrics(dm);
   wPix = dm.widthPixels;
   hPix = dm.heightPixels;
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • So I would have to setup a switch statement for each screen size? – Dave Jan 04 '11 at 23:32
  • Depending on why the position is changed, i think you might be able to do some sort of calculation. As an example: If you're positioning your image always in the center horizontally you would need for a fictional screen of 200 pixels width the location '100'. For 500 you want '250' etc. But instead of a switch you would of course use "width/2". Something similar might be possible for this case? – Nanne Jan 05 '11 at 08:39
  • Can use a ratio as long as you are careful with your rounding so things do not get messy, but design your layouts for a specific resolution, and when the screen its being rendered on isnt at that resolution you would scale the position (and maybe the size if that is important as well) to be more appropriate. – James Jan 05 '11 at 17:44
1

I'm guessing that you are targeting 1.6 or higher. The reason your background looks alright on every device is because Android automatically scales resources to your target screen density when you decode them. You can prevent this by placing your resources in "drawable-nodpi" instead (you'll have to create this directory,) or by passing the appropriate options to the decode bitmap functions (check the documentation.) If you do this though, you'll need to scale all your bitmaps manually. The other two answers have the right idea, grab your screen size and calculate all pixel values. Never use constants.

Keith
  • 21
  • 1