如何获得屏幕密度编程在android?
我的意思是:如何找到当前设备的屏幕dpi ?
如何获得屏幕密度编程在android?
我的意思是:如何找到当前设备的屏幕dpi ?
当前回答
另一种获得设备加载密度的方法:
为每个密度创建值文件夹
值(默认mdpi) values-hdpi values-xhdpi values-xxhdpi values-xxxhdpi
在它们各自的strings.xml中添加一个字符串资源:
<string name="screen_density">MDPI</string> <!-- ..\res\values\strings.xml -->
<string name="screen_density">HDPI</string> <!-- ..\res\values-hdpi\strings.xml -->
<string name="screen_density">XHDPI</string> <!-- ..\res\values-xhdpi\strings.xml -->
<string name="screen_density">XXHDPI</string> <!-- ..\res\values-xxhdpi\strings.xml -->
<string name="screen_density">XXXHDPI</string> <!-- ..\res\values-xxxhdpi\strings.xml -->
然后简单地获取字符串资源,你就有了密度:
String screenDensity = getResources().getString(R.string.screen_density);
如果密度大于XXXHDPI,它将默认为XXXHDPI,如果密度低于HDPI,它将默认为MDPI
我省略了LDPI,因为对于我的用例来说它是不必要的。
其他回答
在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
}
确保定期检查是否增加了新的密度。
另一种获得设备加载密度的方法:
为每个密度创建值文件夹
值(默认mdpi) values-hdpi values-xhdpi values-xxhdpi values-xxxhdpi
在它们各自的strings.xml中添加一个字符串资源:
<string name="screen_density">MDPI</string> <!-- ..\res\values\strings.xml -->
<string name="screen_density">HDPI</string> <!-- ..\res\values-hdpi\strings.xml -->
<string name="screen_density">XHDPI</string> <!-- ..\res\values-xhdpi\strings.xml -->
<string name="screen_density">XXHDPI</string> <!-- ..\res\values-xxhdpi\strings.xml -->
<string name="screen_density">XXXHDPI</string> <!-- ..\res\values-xxxhdpi\strings.xml -->
然后简单地获取字符串资源,你就有了密度:
String screenDensity = getResources().getString(R.string.screen_density);
如果密度大于XXXHDPI,它将默认为XXXHDPI,如果密度低于HDPI,它将默认为MDPI
我省略了LDPI,因为对于我的用例来说它是不必要的。
你应该试试这个。刚刚添加了一个方法,它将找到并显示吐司。设备属于哪个类别。
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;
}
http://www.androidwarriors.com/2016/01/how-to-find-different-devices-screen.html
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;
}
这将在API级别4和更高的级别上工作。
这应该有用。
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels; //320
int height = dm.heightPixels; //480