如何获得真正的屏幕高度和宽度?

时间:2013-01-15 15:29:57

标签: android

DisplayMetrics metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;

我使用这些代码来获取屏幕的高度和宽度

我的Nexus7上的屏幕高度应为1280

但它返回1205 ......

我的minSdkVersion是8级

所以我不能使用这些方法:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screen_width = size.x;
int screen_height = size.y;

现在,我应该如何获得正确的屏幕尺寸?

编辑:

if (Build.VERSION.SDK_INT >= 11) {
        Point size = new Point();
        try {
            this.getWindowManager().getDefaultDisplay().getRealSize(size);
            screenWidth = size.x;
            screenHeight = size.y;
        } catch (NoSuchMethodError e) {
            Log.i("error", "it can't work");
        }

    } else {
        DisplayMetrics metrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;
        screenHeight = metrics.heightPixels;
    }

使用它有效!

3 个答案:

答案 0 :(得分:45)

我已经编写了一个尽可能接近API 10到17+的方法。它应该为API 14+提供真正的宽度和高度,并且它可以用于其他所有内容。

    Display display = context.getWindowManager().getDefaultDisplay();
    int realWidth;
    int realHeight;

    if (Build.VERSION.SDK_INT >= 17){
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
        realHeight = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
            realHeight = display.getHeight();
            Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
        realHeight = display.getHeight();
    }

答案 1 :(得分:35)

我认为这会奏效。诀窍是你必须使用:

display.getRealSize(size);

display.getSize(size);

要处理您的API 8编码问题,请执行以下操作:

try { 
    display.getRealSize(size);
    height = size.y; 
} catch (NoSuchMethodError e) {
    height = display.getHeight();
}

只有更新的API设备会有屏幕导航按钮,因此需要新方法,旧设备会抛出异常,但不会有屏幕导航,因此较旧的方法很好。

如果需要说:只是因为你的项目的最低API级别为8并不意味着你必须在那个级别编译它。我也使用8级作为最低限度,但我的项目大多是在13级(3.2)编译,使他们可以访问许多新方法。

答案 2 :(得分:2)

您可以使用DisplayDisplayMetrics

找到实际设备尺寸

使用展示

private void findRealSize(Activity activity)
{
    Point size = new Point();
    Display display = activity.getWindowManager().getDefaultDisplay();

    if (Build.VERSION.SDK_INT >= 17)
        display.getRealSize(size);
    else
        display.getSize(size);

    int realWidth = size.x;
    int realHeight = size.y;

    Log.i("LOG_TAG", "realWidth: " + realWidth + " realHeight: " + realHeight);
}

使用DisplayMetrics:

private void findRealSize(Activity activity)
{
    DisplayMetrics displayMetrics = new DisplayMetrics();

    if (Build.VERSION.SDK_INT >= 17)
    {
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
    }
    else
    {
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    }

    int realWidth = displayMetrics.widthPixels;
    int realHeight = displayMetrics.heightPixels;

    Log.i("LOG_TAG", "realWidth: " + realWidth + " realHeight: " + realHeight);
}