我在支持不同的设备及其屏幕时遇到了问题:我有一个游戏,我在网格中绘制了很多 70px*70px 的图标。
.png 文件为 70*70 @ 315ppi。
在我的 java 代码中,我现在使用以下代码将图像绘制到网格中:
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);
}
}
(x*70) 在我的 Galaxy Nexus 上运行良好,但是当我在 hdpi 800 的设备上测试它时,70 像素的值太高了。
使此值适应不同屏幕的最佳方法是什么? 感谢您帮助我!
最佳答案
将 X 和 Y 与适当的比例因子相乘。
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) 等...
关于Android:使用 SurfaceView 时如何支持多种屏幕分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11580649/