简体   繁体   English

Nexus 7的实际屏幕尺寸

[英]Nexus 7 actual screen size

I built a simple application for Nexus 7. I used the following code to get the screen size in DP units. 我为Nexus 7构建了一个简单的应用程序。我使用以下代码来获取DP单位的屏幕尺寸。

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

where "this" is MainActivity context object. 其中“ this”是MainActivity上下文对象。

I get these values: 600 dp for width and 888 dp for height. 我得到以下值:宽度为600 dp,高度为888 dp。

Pixel density is tvdpi which is 213, and the ratio of dp to pixels is 1.33 像素密度是tvdpi ,它是213,dp与像素的比率是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? 我知道Nexus 7屏幕的像素大小为800 x1280。此计算中缺少的100个像素高度在哪里? Or did I do something wrong? 还是我做错了什么?

Configuration.screenHeightDp() returns the dimensions of the available area of the screen. Configuration.screenHeightDp()返回屏幕可用区域的尺寸。

Your calculated value, 1182, is close to the the height in pixels minus the navigation bar and status bar (1173) of the Nexus 7, in other words the resolution available for your app to use. 计算得出的值1182接近于以像素为单位的高度减去 Nexus 7 的导航栏和状态栏 (1173),换句话说,就是您的应用可以使用的分辨率。

Full screen apps should be able to use the full 1280 resolution. 全屏应用程序应该能够使用完整的1280分辨率。

The following should give you the actual display size as a Point: 以下应该为您提供实际的显示大小(以点为单位):

    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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM