How to get actual screen size after Android N?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 19:02:42

问题


Since Android N introduces split screen, the window size of your app can be half of the original. I found that getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); no longer always returns the actual device screen size anymore because in split screen mode the display's height will be your app's window height instead. Is there any other API I can use to get the actual screen size?


回答1:


Display.getRealMetrics(DisplayMetrics) will return the actual display size.




回答2:


You can get real device size in Multi-Window Mode using Display or DisplayMetrics.

Using Display:

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

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



回答3:


By the moment I couldn't find a proper solution for that at all, but I'm handling it by the densityDpi of every resolution which is changed. Just like this:

private int getScreenDensityDPI() {
    DisplayMetrics metrics = new DisplayMetrics();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
    } else {
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
    }
    return metrics.densityDpi;
}

private int getNumberItemsByScreenResolution() {
    int densityDPI = getScreenDensityDPI();
    if (densityDPI <= 140) {
        return 6;
    } else if (densityDPI <= 160) {
        return 5;
    } else {
        return 4;
    }
}

While lowest the densityDPI is, highest the resolution of the display will be in your android N device.



来源:https://stackoverflow.com/questions/36706365/how-to-get-actual-screen-size-after-android-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!