1. 程式人生 > >Android手機螢幕尺寸計算

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