1、 精确获取屏幕尺寸(例如:3.5、4.0、5.0寸屏幕)
public static double getScreenPhysicalSize(Activity ctx) {
DisplayMetrics dm = new DisplayMetrics();
ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
return diagonalPixels / (160 * dm.density);
}
2、 利用Configuration获取屏幕是small/normal/large/xlarge中的哪一个
Configuration cfg = getResources().getConfiguration();
int size = cfg.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
switch (size) {
case Configuration.SCREENLAYOUT_SIZE_UNDEFINED:
screenSize = "undefined";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
screenSize = "small";
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
screenSize = "normal";
break;
case Configuration.SCREENLAYOUT_SIZE_LARGE:
screenSize = "large";
break;
case Configuration.SCREENLAYOUT_SIZE_XLARGE:
screenSize = "xlarge";
break;
default:
screenSize = "normal";
}