DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// will either be DENSITY_LOW, DENSITY_MEDIUM or DENSITY_HIGH
int dpiClassification = dm.densityDpi;
// these will return the actual dpi horizontally and vertically
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi) {
case DisplayMetrics.DENSITY_LOW:
break;
case DisplayMetrics.DENSITY_MEDIUM:
break;
case DisplayMetrics.DENSITY_HIGH:
break;
}
Device: Sony ericsson xperia mini pro (SK17i)
Density: 1.0 (e.g. suggests you use 160dpi resources)
xdpi: 193.5238
Real device ppi is arround 193ppi
Device: samsung GT-I8160 (Samsung ace 2)
Density 1.5 (e.g. suggests you use 240dpi resources)
xdpi 160.42105
Real device ppi is arround 246ppi
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;
}
public static int differentDensityAndScreenSize(Context context) {
int value = 20;
String str = "";
if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
str = "small-ldpi";
value = 20;
break;
case DisplayMetrics.DENSITY_MEDIUM:
str = "small-mdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_HIGH:
str = "small-hdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_XHIGH:
str = "small-xhdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_XXHIGH:
str = "small-xxhdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
str = "small-xxxhdpi";
value = 20;
break;
case DisplayMetrics.DENSITY_TV:
str = "small-tvdpi";
value = 20;
break;
default:
str = "small-unknown";
value = 20;
break;
}
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
str = "normal-ldpi";
value = 82;
break;
case DisplayMetrics.DENSITY_MEDIUM:
str = "normal-mdpi";
value = 82;
break;
case DisplayMetrics.DENSITY_HIGH:
str = "normal-hdpi";
value = 82;
break;
case DisplayMetrics.DENSITY_XHIGH:
str = "normal-xhdpi";
value = 90;
break;
case DisplayMetrics.DENSITY_XXHIGH:
str = "normal-xxhdpi";
value = 96;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
str = "normal-xxxhdpi";
value = 96;
break;
case DisplayMetrics.DENSITY_TV:
str = "normal-tvdpi";
value = 96;
break;
default:
str = "normal-unknown";
value = 82;
break;
}
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
str = "large-ldpi";
value = 78;
break;
case DisplayMetrics.DENSITY_MEDIUM:
str = "large-mdpi";
value = 78;
break;
case DisplayMetrics.DENSITY_HIGH:
str = "large-hdpi";
value = 78;
break;
case DisplayMetrics.DENSITY_XHIGH:
str = "large-xhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XXHIGH:
str = "large-xxhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
str = "large-xxxhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_TV:
str = "large-tvdpi";
value = 125;
break;
default:
str = "large-unknown";
value = 78;
break;
}
} else if ((context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
switch (context.getResources().getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
str = "xlarge-ldpi";
value = 125;
break;
case DisplayMetrics.DENSITY_MEDIUM:
str = "xlarge-mdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_HIGH:
str = "xlarge-hdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XHIGH:
str = "xlarge-xhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XXHIGH:
str = "xlarge-xxhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_XXXHIGH:
str = "xlarge-xxxhdpi";
value = 125;
break;
case DisplayMetrics.DENSITY_TV:
str = "xlarge-tvdpi";
value = 125;
break;
default:
str = "xlarge-unknown";
value = 125;
break;
}
}
// The Toast will show the Device falls in Which Categories.
Toast.makeText(MainActivity.this, ""+str, Toast.LENGTH_SHORT).show();
return value;
}
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
}