2012-07-20 62 views
1

我在支持不同设备及其屏幕时遇到问题:我有游戏,我在网格中绘制了很多70px * 70px的图标。Android:如何在使用SurfaceView时支持多种屏幕分辨率

.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的70px值太高了。

将这个值调整到不同屏幕的最佳方法是什么? 感谢您的帮助!

回答

3

用合适的比例因子相乘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)等等

+0

谢谢,我想到的是这样了。似乎是最好的办法,谢谢! – Heisenbug 2012-07-20 16:36:08