1

I built a simple application for Nexus 7. I used the following code to get the screen size in DP units.

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

where "this" is MainActivity context object.

I get these values: 600 dp for width and 888 dp for height.

Pixel density is tvdpi which is 213, and the ratio of dp to pixels is 1.33

I used this formula

pixels = dips * (density / 160) 

which gives me for height

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

I know that pixel size of the Nexus 7 screen is 800 x 1280. Where are the missing 100 pixels of height in this calculation? Or did I do something wrong?

4

2 回答 2

3

Configuration.screenHeightDp()返回屏幕可用区域的尺寸。

您的计算值 1182 接近于以像素为单位减去 Nexus 7 的导航栏和状态栏(1173) 的高度,换言之,您的应用程序可以使用的分辨率。

全屏应用程序应该能够使用完整的 1280 分辨率。

于 2013-07-28T16:24:53.707 回答
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;
}
于 2013-07-28T21:26:22.630 回答