2011-08-01 28 views
1

我正在製作遊戲並使用SurfaceView。 我加載代表字符和背景的位圖等等。Android開發:縮放以適應不同的設備

但是我該如何正確調整它以適應大型設備和小型設備以及其他設備?

//西蒙

注* DPI不會工作,只有象素的作用whhen用帆布

回答

1

您可以基於此qustion @MitulNakum答案實現,你可以使用基於設備的DPI,請求計算一個輔助方法

,這樣做:

//note that mdpi is the standard 
float getAdjustedDimension(float value){ 
    DisplayMetrics metrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    switch(metrics.densityDpi){ 
     case DisplayMetrics.DENSITY_LOW: 
       //api 4 and higher 
       return 0.75 * value; 
     case DisplayMetrics.DENSITY_MEDIUM: 
       //api 4 and higher 
       return value; 
     case DisplayMetrics.DENSITY_HIGH: 
       //api 4 and higher 
       return value * 1.25; 
     case DisplayMetrics.DENSITY_XHIGH 
       //api 9 and higher 
       return value * 2; 
     case DisplayMetrics.DENSITY_TV 
       //api 13 and higher 
       return value * 1.33125; 
    } 
} 
+0

兩點很重要,你應該緩存metrics.densityDpi,這不是一個遊戲非常有效的。 – Gallal

+0

爲什麼不返回metrics.density *值? – MrJre

0

Android的支持方式是通過爲每個你需要爲你的不同大小和讓的應用程序創建位圖圖像數應用程序決定使用哪個屏幕密度。這些圖像將放入drawable-ldpi,drawable-hdpi等文件夾中。查看鏈接並仔細閱讀:http://developer.android.com/guide/practices/screens_support.html

雖然不是非常有記憶力。 。:(

1

乘坐看看AndEngine的相機,你所需要的一切都可以在那裏找到,而且它是開源的!我已經將這個引擎用於許多遊戲和原型應用程序,它有很好的文檔記錄。

Source Code

+1

對不起,但我沒有inttitted在引擎:) – carefacerz