开发者

Display image in the same spot on multiple screens | Android

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 exam开发者_如何转开发ple 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?


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;


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜