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

我的意思是:如何找到当前设备的屏幕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;

其他回答

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和更高的级别上工作。

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

为每个密度创建值文件夹

值(默认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,因为对于我的用例来说它是不必要的。

试试这个…

在kotlin

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。输出:密度:xxxhdpi

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

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

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

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