问题
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