2013-07-28 119 views
1

我爲Nexus 7構建了一個簡單的應用程序。我使用以下代碼獲取DP單元的屏幕尺寸。Nexus 7實際屏幕尺寸

this.getResources().getConfiguration().screenWidthDp; 
this.getResources().getConfiguration().screenHeightDp; 

其中「this」是MainActivity上下文對象。

我得到這些值:600 DP寬度和888 DP的高度。

像素密度是tvdpi它是213,和DP的像素的比率爲1.33

我用這個公式

pixels = dips * (density/160) 

這使我要高度

pixels = 888 * (213/160) = 1182.15. 

我知道Nexus 7屏幕的像素尺寸爲800 x 1280.在此計算中,缺失的100個像素高度在哪裏?還是我做錯了什麼?

回答

2

下應該給你的實際顯示尺寸爲點:

private Point getDisplaySize(Context context) { 

    if (Build.VERSION.SDK_INT >= 17) { 
     return getDisplaySizeMinSdk17(context); 
    } else if (Build.VERSION.SDK_INT >= 13) { 
     return getDisplaySizeMinSdk13(context); 
    } else { 
     return getDisplaySizeMinSdk1(context); 
    } 
} 

@TargetApi(17) 
private Point getDisplaySizeMinSdk17(Context context) { 
    final WindowManager windowManager = (WindowManager) context 
      .getSystemService(Context.WINDOW_SERVICE); 
    final Display display = windowManager.getDefaultDisplay(); 

    final DisplayMetrics metrics = new DisplayMetrics(); 
    display.getRealMetrics(metrics); 

    final Point size = new Point(); 
    size.x = metrics.widthPixels; 
    size.y = metrics.heightPixels; 

    return size; 
} 

@TargetApi(13) 
private Point getDisplaySizeMinSdk13(Context context) { 

    final WindowManager windowManager = (WindowManager) context 
      .getSystemService(Context.WINDOW_SERVICE); 
    final Display display = windowManager.getDefaultDisplay(); 

    final Point size = new Point(); 
    display.getSize(size); 

    return size; 
} 

@SuppressWarnings("deprecation") 
private Point getDisplaySizeMinSdk1(Context context) { 

    final WindowManager windowManager = (WindowManager) context 
      .getSystemService(Context.WINDOW_SERVICE); 
    final Display display = windowManager.getDefaultDisplay(); 

    final Point size = new Point(); 
    size.x = display.getWidth(); 
    size.y = display.getHeight(); 

    return size; 
} 
+0

完美的作品,謝謝:) – Vlad