Android:如何在使用SurfaceView时支持多个屏幕分辨率
我在支持不同的设备和它们的屏幕时遇到了一个问题:我有一个游戏,我在网格中画了很多70px*70px的图标 .png文件是70*70@315ppi 在java代码中,我现在使用以下代码将图像绘制到网格:Android:如何在使用SurfaceView时支持多个屏幕分辨率,android,image,bitmap,scaling,resolutions,Android,Image,Bitmap,Scaling,Resolutions,我在支持不同的设备和它们的屏幕时遇到了一个问题:我有一个游戏,我在网格中画了很多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); ba
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的值太高了
使该值适应不同屏幕的最佳方法是什么?
谢谢你帮助我 用适当的比例因子乘以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.setXx*70*scaleX等。用适当的比例因子乘以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.setXx*70*scaleX等。请参考
https://developer.android.com/training/multiscreen/screendensities
你会看到的
36x36 (0.75x) for low-density (ldpi)
48x48 (1.0x baseline) for medium-density (mdpi)
72x72 (1.5x) for high-density (hdpi)
96x96 (2.0x) for extra-high-density (xhdpi)
144x144 (3.0x) for extra-extra-high-density (xxhdpi)
192x192 (4.0x) for extra-extra-extra-high-density (xxxhdpi)
您将看到0.75x到4.0x,我非常确定您将使用这些值来缩放屏幕密度的UI对象。你可能会发现更多,但这些是主要的
PS新设备不使用从mdpi到xxxhdpi的连接。您将在链接中看到这一点。请参考
https://developer.android.com/training/multiscreen/screendensities
你会看到的
36x36 (0.75x) for low-density (ldpi)
48x48 (1.0x baseline) for medium-density (mdpi)
72x72 (1.5x) for high-density (hdpi)
96x96 (2.0x) for extra-high-density (xhdpi)
144x144 (3.0x) for extra-extra-high-density (xxhdpi)
192x192 (4.0x) for extra-extra-extra-high-density (xxxhdpi)
您将看到0.75x到4.0x,我非常确定您将使用这些值来缩放屏幕密度的UI对象。你可能会发现更多,但这些是主要的
PS新设备不使用从mdpi到xxxhdpi的连接。您将在链接中看到这一点。谢谢,我也这么想。看来这是最好的办法,谢谢!谢谢,我也是这么想的。看来这是最好的办法,谢谢!