在android中以编程方式获得屏幕密度?

如何获得屏幕密度编程在android?

我的意思是:如何找到当前设备的屏幕dpi ?

341300 次浏览

这应该有用。

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常量之一(120160213240320480640 dpi)。

如果你需要实际液晶像素密度(也许对于OpenGL应用程序),你可以分别从metrics.xdpimetrics.ydpi属性中获得水平和垂直密度。

如果你的目标API级别早于4。metrics.density属性是参考密度的浮点比例因子(160dpi)。现在可以计算由metrics.densityDpi提供的相同值

int densityDpi = (int)(metrics.density * 160f);

得到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;
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 = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
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;
}

如果你想从服务中检索密度,它是这样工作的:

WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);

这也是可行的:

 getResources().getDisplayMetrics().density;

这将给你:

0.75 - ldpi

1.0 - mdpi

1.5 - hdpi

2.0 - xhdpi

3.0 - xxhdpi

4.0 - xxxhdpi

enter image description here

ref: density

enter image description here

ref 2

这应该有助于你的活动……

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);


}

输出:

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

布兰戴尔的 answer作为静态帮助方法:

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";
}
实际上,如果你想要实显dpi,答案是介于两者之间 如果查询显示指标:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int dpiClassification = dm.densityDpi;
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;

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

如之前的文章所述

但是dm.xdpi不会总是给你给定显示的实际个人可支配收入: 例子:< / p >

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

所以可能显示器的真实dpi应该是密度*xdpi ..但我不确定这是否是正确的做法!

下面的答案是基于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
}

另一个答案是:

/**
* @return "ldpi", "mdpi", "hdpi", "xhdpi", "xhdpi", "xxhdpi", "xxxhdpi", "tvdpi", or "unknown".
*/
public static String getDensityBucket(Resources resources) {
switch (resources.getDisplayMetrics().densityDpi) {
case DisplayMetrics.DENSITY_LOW:
return "ldpi";
case DisplayMetrics.DENSITY_MEDIUM:
return "mdpi";
case DisplayMetrics.DENSITY_HIGH:
return "hdpi";
case DisplayMetrics.DENSITY_XHIGH:
return "xhdpi";
case DisplayMetrics.DENSITY_XXHIGH:
return "xxhdpi";
case DisplayMetrics.DENSITY_XXXHIGH:
return "xxxhdpi";
case DisplayMetrics.DENSITY_TV:
return "tvdpi";
default:
return "unknown";
}
}

这里有一些密度常数,:

enter image description here

除了标准密度之外,还有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;
}

你应该试试这个。刚刚添加了一个方法,它将找到并显示吐司。设备属于哪个类别。

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

试试这个…

在芬兰湾的科特林

fun determineScreenDensityCode(): String {
return when (resources.displayMetrics.densityDpi) {
DisplayMetrics.DENSITY_LOW -> "ldpi"
DisplayMetrics.DENSITY_MEDIUM -> "mdpi"
DisplayMetrics.DENSITY_HIGH -> "hdpi"
DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_280 -> "xhdpi"
DisplayMetrics.DENSITY_XXHIGH, DisplayMetrics.DENSITY_360, DisplayMetrics.DENSITY_400, DisplayMetrics.DENSITY_420 -> "xxhdpi"
DisplayMetrics.DENSITY_XXXHIGH, DisplayMetrics.DENSITY_560 -> "xxxhdpi"
else -> "Unknown code ${resources.displayMetrics.densityDpi}"
}
}

您可以通过println("density: ${determineScreenDensityCode()}")调用 输出将是System.out: density: xxxhdpi

我使用以下代码从模块访问DPI(不需要访问上下文对象):

(Resources.getSystem().getDisplayMetrics().xdpi
Resources.getSystem().getDisplayMetrics().ydpi)/2

另一种获得设备加载密度的方法:

为每个密度创建values文件夹

  • 值(默认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

R.strings。screen_density价值观”> < / ></p>


<p>我省略了<code>LDPI</code>,因为对于我的用例来说它是不必要的。</p></div>
                                                                            </div>
                                </div>
                            </div>
                        </div>
                                                <div class=

在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
}

确保定期检查是否添加了新的密度

芬兰湾的科特林

# EYZ0

得到密度乘数。