getting the screen density programmatically in android?

后端 未结 19 2674
感动是毒
感动是毒 2020-11-22 01:56

How to get the screen density programmatically in android?

I mean: How to find the screen dpi of the current device?

19条回答
  •  伪装坚强ぢ
    2020-11-22 02:24

    Yet another answer:

    /**
    * @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";
        }
    }
    

提交回复
热议问题