如何获得屏幕密度编程在android?
我的意思是:如何找到当前设备的屏幕dpi ?
如何获得屏幕密度编程在android?
我的意思是:如何找到当前设备的屏幕dpi ?
当前回答
以下是一些密度常数,来源:
除了标准密度之外,还有5种中间密度。考虑到这一点,下面的代码将是一个完整的工作示例:
float density = getResources().getDisplayMetrics().density;
if (density == 0.75f)
{
// LDPI
}
else if (density >= 1.0f && density < 1.5f)
{
// MDPI
}
else if (density == 1.5f)
{
// HDPI
}
else if (density > 1.5f && density <= 2.0f)
{
// XHDPI
}
else if (density > 2.0f && density <= 3.0f)
{
// XXHDPI
}
else
{
// XXXHDPI
}
或者,你可以使用densityDpi找到密度常数:
int densityDpi = getResources().getDisplayMetrics().densityDpi;
switch (densityDpi)
{
case DisplayMetrics.DENSITY_LOW:
// LDPI
break;
case DisplayMetrics.DENSITY_MEDIUM:
// MDPI
break;
case DisplayMetrics.DENSITY_TV:
case DisplayMetrics.DENSITY_HIGH:
// HDPI
break;
case DisplayMetrics.DENSITY_XHIGH:
case DisplayMetrics.DENSITY_280:
// XHDPI
break;
case DisplayMetrics.DENSITY_XXHIGH:
case DisplayMetrics.DENSITY_360:
case DisplayMetrics.DENSITY_400:
case DisplayMetrics.DENSITY_420:
// XXHDPI
break;
case DisplayMetrics.DENSITY_XXXHIGH:
case DisplayMetrics.DENSITY_560:
// XXXHDPI
break;
}
其他回答
这应该有用。
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //320
int height = dm.heightPixels; //480
你可以从DisplayMetrics结构体中获取显示信息:
DisplayMetrics metrics = getResources().getDisplayMetrics();
虽然Android不使用直接的像素映射,但它使用了少量量化的密度无关像素值,然后缩放到实际的屏幕尺寸。因此metrics.densityDpi属性将是DENSITY_xxx常量之一(120,160,213,240,320,480或640 dpi)。
如果你需要实际的lcd像素密度(也许对于OpenGL应用程序),你可以从指标中得到它。Xdpi和度量。水平密度和垂直密度的Ydpi特性。
如果你的目标API级别早于4。metrics.density属性是一个来自参考密度(160dpi)的浮点比例因子。现在可以计算metrics.densityDpi提供的相同值
int densityDpi = (int)(metrics.density * 160f);
public static String getDensity(Context context) {
String r;
DisplayMetrics metrics = new DisplayMetrics();
if (!(context instanceof Activity)) {
r = "hdpi";
} else {
Activity activity = (Activity) context;
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (metrics.densityDpi <= DisplayMetrics.DENSITY_LOW) {
r = "ldpi";
} else if (metrics.densityDpi <= DisplayMetrics.DENSITY_MEDIUM) {
r = "mdpi";
} else {
r = "hdpi";
}
}
return r;
}
科特林
LocalContext.current.resources.displayMetrics.density
得到密度乘数。
在Android中,你可以得到这样的屏幕密度:
public static String getScreenDensity(Context context)
{
String density;
switch (context.getResources().getDisplayMetrics().densityDpi)
{
case DisplayMetrics.DENSITY_LOW:
density = "LDPI";
break;
case DisplayMetrics.DENSITY_140:
density = "LDPI - MDPI";
break;
case DisplayMetrics.DENSITY_MEDIUM:
density = "MDPI";
break;
case DisplayMetrics.DENSITY_180:
case DisplayMetrics.DENSITY_200:
case DisplayMetrics.DENSITY_220:
density = "MDPI - HDPI";
break;
case DisplayMetrics.DENSITY_HIGH:
density = "HDPI";
break;
case DisplayMetrics.DENSITY_260:
case DisplayMetrics.DENSITY_280:
case DisplayMetrics.DENSITY_300:
density = "HDPI - XHDPI";
break;
case DisplayMetrics.DENSITY_XHIGH:
density = "XHDPI";
break;
case DisplayMetrics.DENSITY_340:
case DisplayMetrics.DENSITY_360:
case DisplayMetrics.DENSITY_400:
case DisplayMetrics.DENSITY_420:
case DisplayMetrics.DENSITY_440:
density = "XHDPI - XXHDPI";
break;
case DisplayMetrics.DENSITY_XXHIGH:
density = "XXHDPI";
break;
case DisplayMetrics.DENSITY_560:
case DisplayMetrics.DENSITY_600:
density = "XXHDPI - XXXHDPI";
break;
case DisplayMetrics.DENSITY_XXXHIGH:
density = "XXXHDPI";
break;
case DisplayMetrics.DENSITY_TV:
density = "TVDPI";
break;
default:
density = "UNKNOWN";
break;
}
return density;
}
在Kotlin中是这样的:
fun getScreenDensity(context: Context): String {
val density: String
when (context.resources.displayMetrics.densityDpi) {
DisplayMetrics.DENSITY_LOW -> density = "LDPI"
DisplayMetrics.DENSITY_140 -> density = "LDPI - MDPI"
DisplayMetrics.DENSITY_MEDIUM -> density = "MDPI"
DisplayMetrics.DENSITY_180, DisplayMetrics.DENSITY_200, DisplayMetrics.DENSITY_220 -> density = "MDPI - HDPI"
DisplayMetrics.DENSITY_HIGH -> density = "HDPI"
DisplayMetrics.DENSITY_260, DisplayMetrics.DENSITY_280, DisplayMetrics.DENSITY_300 -> density = "HDPI - XHDPI"
DisplayMetrics.DENSITY_XHIGH -> density = "XHDPI"
DisplayMetrics.DENSITY_340, DisplayMetrics.DENSITY_360, DisplayMetrics.DENSITY_400, DisplayMetrics.DENSITY_420, DisplayMetrics.DENSITY_440 -> density =
"XHDPI - XXHDPI"
DisplayMetrics.DENSITY_XXHIGH -> density = "XXHDPI"
DisplayMetrics.DENSITY_560, DisplayMetrics.DENSITY_600 -> density = "XXHDPI - XXXHDPI"
DisplayMetrics.DENSITY_XXXHIGH -> density = "XXXHDPI"
DisplayMetrics.DENSITY_TV -> density = "TVDPI"
else -> density = "UNKNOWN"
}
return density
}
确保定期检查是否增加了新的密度。