@author 刘长炯 2016.01.09
Code License: Apache
You might also want read this: http://wiki.jikexueyuan.com/project/android-actual-combat-skills/acquring-screen-physical-size-density-and-resolution.html
/**
* 获取屏幕对角大小的寸数.
*
* @param ctx Context
* @return 屏幕对角寸数
*/
public static double getScreenInchSize(Context ctx) {
WindowManager windowManager = (WindowManager)
ctx.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
Point point = new Point();
// activity.getWindowManager().getDefaultDisplay().getRealSize(point);
getRealScreenSize(ctx, point);
DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
double x = Math.pow(point.x / dm.xdpi, 2);
double y = Math.pow(point.y / dm.ydpi, 2);
double screenInches = Math.sqrt(x + y);
return screenInches;
}

return ;
}

/**
* 获取屏幕真正大小, 包含smartbar等.
* Ref: http://stackoverflow.com/questions/13628389/android-how-to-get-the-real-screen-size-of-the-device
*
* @param context
* @param point
*/
public static void getRealScreenSize(Context context, Point point) {
WindowManager windowManager = (WindowManager)
context.getSystemService(Context.WINDOW_SERVICE);
if (windowManager != null) {
Display d = windowManager.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
// since SDK_INT = 1;
int widthPixels = metrics.widthPixels;
int heightPixels = metrics.heightPixels;
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 14 && Build.VERSION.SDK_INT < 17) {
try {
widthPixels = (Integer) Display.class.getMethod("getRawWidth").invoke(d);
heightPixels = (Integer) Display.class.getMethod("getRawHeight").invoke(d);
} catch (Exception ignored) {
}
}
// includes window decorations (statusbar bar/menu bar)
if (Build.VERSION.SDK_INT >= 17) {
try {
Point realSize = new Point();
Display.class.getMethod("getRealSize", Point.class).invoke(d, realSize);
widthPixels = realSize.x;
heightPixels = realSize.y;
} catch (Exception ignored) {
}
}

point.x = widthPixels;
point.y = heightPixels;
}
}

转载请注明:WebLogic Android 博客 » Android 获取屏幕大小寸数(含smartbar)