【问题标题】:Android: How to support multiple screen resolutions when using SurfaceViewAndroid:使用 SurfaceView 时如何支持多种屏幕分辨率
【发布时间】:2012-07-19 19:21:58
【问题描述】:

我在支持不同的设备及其屏幕时遇到了问题:我有一个游戏,我在一个网格中绘制了很多 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 的设备上测试它时,70px 的值太高了。

使该值适应不同屏幕的最佳方法是什么? 感谢您帮助我!

【问题讨论】:

    标签: android image bitmap scaling resolutions


    【解决方案1】:

    请自行参考

     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。您将在链接中看到。

    【讨论】:

      【解决方案2】:

      将 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) 等...

      【讨论】:

      • 谢谢 我也是这么想的。看来这是最好的方法了,谢谢!
      猜你喜欢
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多