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";
}
本文介绍如何使用Java代码精确获取Android设备的屏幕尺寸(单位为寸),包括3.5、4.0、5.0等常见尺寸,并通过Configuration获取屏幕大小类别,如small、normal、large、xlarge等。
813

被折叠的 条评论
为什么被折叠?



