Android手机屏幕尺寸计算

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    display.getRealMetrics(dm);
}else {
    display.getMetrics(dm);
}
double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
// 屏幕尺寸
BigDecimal decimal = new BigDecimal(Math.sqrt(x + y));
decimal = decimal.setScale(1,BigDecimal.ROUND_UP);
double mScreenInches = decimal.doubleValue();

你可能感兴趣的:(Android学习笔记)