在android中以编程方式获取屏幕密度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3166501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting the screen density programmatically in android?
提问by Praveen
How to get the screen density programmatically in android?
如何在android中以编程方式获取屏幕密度?
I mean: How to find the screen dpi of the current device?
我的意思是:如何找到当前设备的屏幕 dpi?
回答by joshperry
You can get info on the display from the DisplayMetricsstruct:
您可以从DisplayMetrics结构中获取有关显示的信息:
DisplayMetrics metrics = getResources().getDisplayMetrics();
Though Android doesn't use a direct pixel mapping, it uses a handful of quantized Density Independent Pixel values then scales to the actual screen size. So the metrics.densityDpiproperty will be one of the DENSITY_xxxconstants (120, 160, 213, 240, 320, 480or 640dpi).
尽管 Android 不使用直接像素映射,但它使用少量量化的密度独立像素值,然后缩放到实际屏幕尺寸。因此,metrics.densityDpi物业将是一个DENSITY_xxx常数(120,160,213,240,320,480或640DPI)。
If you need the actuallcd pixel density (perhaps for an OpenGL app) you can get it from the metrics.xdpiand metrics.ydpiproperties for horizontal and vertical density respectively.
如果您需要实际的lcd 像素密度(也许对于 OpenGL 应用程序),您可以分别从水平和垂直密度的metrics.xdpi和metrics.ydpi属性中获取它。
If you are targeting API Levels earlier than 4. The metrics.densityproperty is a floating point scaling factor from the reference density (160dpi). The same value now provided by metrics.densityDpican be calculated
如果您的目标 API 级别早于 4。该metrics.density属性是参考密度 (160dpi) 的浮点缩放因子。metrics.densityDpi可以计算现在提供的相同值
int densityDpi = (int)(metrics.density * 160f);
回答by Blundell
This also works:
这也有效:
getResources().getDisplayMetrics().density;
This will give you:
这会给你:
0.75 - ldpi
0.75 - 低密度脂蛋白
1.0 - mdpi
1.0 - mdpi
1.5 - hdpi
1.5 - hdpi
2.0 - xhdpi
2.0 - xhdpi
3.0 - xxhdpi
3.0 - xxhdpi
4.0 - xxxhdpi
4.0 - xxxhdpi


回答by Mitul Nakum
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;
}
This will work on API level 4 and higher.
这将适用于 API 级别 4 及更高级别。
回答by qwertzguy
Blundell'sanswer as a static helper method:
private static String getDensityName(Context context) {
float density = context.getResources().getDisplayMetrics().density;
if (density >= 4.0) {
return "xxxhdpi";
}
if (density >= 3.0) {
return "xxhdpi";
}
if (density >= 2.0) {
return "xhdpi";
}
if (density >= 1.5) {
return "hdpi";
}
if (density >= 1.0) {
return "mdpi";
}
return "ldpi";
}
回答by Wizist
Try this:
尝试这个:
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
回答by Jere.Jones
To get dpi:
获取 dpi:
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;
回答by Ayaz Alifov
Here are the density constants, source:
这是密度常数,来源:
There are, in addition to the standard densities, 5 Intermediate ones. Taking into account this fact, the following code will be a complete working example:
除了标准密度外,还有 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
}
Alternatively, you can find density constants using the densityDpi:
或者,您可以使用以下命令找到密度常数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;
}
回答by San
The following answer is a small improvement based upon qwertzguy's answer.
以下答案是基于 qwertzguy 的答案的一个小改进。
double density = getResources().getDisplayMetrics().density;
if (density >= 4.0) {
//"xxxhdpi";
}
else if (density >= 3.0 && density < 4.0) {
//xxhdpi
}
else if (density >= 2.0) {
//xhdpi
}
else if (density >= 1.5 && density < 2.0) {
//hdpi
}
else if (density >= 1.0 && density < 1.5) {
//mdpi
}
回答by Marek Halmo
Actualy if you want to have the real display dpithe answer is somewhere in between if you query for display metrics:
实际上,如果您想获得真正的显示 dpi,那么如果您查询显示指标,答案就介于两者之间:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int dpiClassification = dm.densityDpi;
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;
densityDpi * 160 will give you the values/suggestion which density you should use
densityDpi * 160 会给你值/建议你应该使用哪种密度
0.75 - ldpi - 120 dpi
1.0 - mdpi - 160 dpi
1.5 - hdpi - 240 dpi
2.0 - xhdpi - 320 dpi
3.0 - xxhdpi - 480 dpi
4.0 - xxxhdpi - 640 dpi
as specified in previous posts
如以前的帖子所述
but dm.xdpiwon't give you always the REAL dpiof given display:
Example:
但dm.xdpi不会始终为您提供给定显示的真实 dpi:示例:
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
so maybe real dpi of the display should be Density*xdpi .. but i'm not sure if this is the correct way to do!
所以也许显示器的真实 dpi 应该是 Density*xdpi .. 但我不确定这是否是正确的方法!
回答by prabhu
This should help on your activity ...
这应该有助于您的活动...
void printSecreenInfo(){
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Log.i(TAG, "density :" + metrics.density);
// density interms of dpi
Log.i(TAG, "D density :" + metrics.densityDpi);
// horizontal pixel resolution
Log.i(TAG, "width pix :" + metrics.widthPixels);
// actual horizontal dpi
Log.i(TAG, "xdpi :" + metrics.xdpi);
// actual vertical dpi
Log.i(TAG, "ydpi :" + metrics.ydpi);
}
OUTPUT :
输出 :
I/test( 1044): density :1.0
I/test( 1044): D density :160
I/test( 1044): width pix :800
I/test( 1044): xdpi :160.0
I/test( 1044): ydpi :160.42105

