简体   繁体   English

在多个屏幕上的同一位置显示图像| Android的

[英]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 example if I use 例如,如果我使用

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

It will align the image perfectly for phones that have 854px width. 对于宽度为854像素的手机,它会完美地对齐图像。 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. 我猜您的目标是1.6或更高。 The reason your background looks alright on every device is because Android automatically scales resources to your target screen density when you decode them. 您的背景在每台设备上看起来都不错的原因是因为Android在解码时会自动将资源调整到目标屏幕密度。 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. 您可以通过将资源放置在“ drawable-nodpi”中(必须创建此目录)或将适当的选项传递给解码位图函数(请参阅文档)来防止这种情况。将需要手动缩放所有位图。 The other two answers have the right idea, grab your screen size and calculate all pixel values. 其他两个答案都有正确的想法,抓住你的屏幕尺寸并计算所有像素值。 Never use constants. 永远不要使用常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM