getting the screen density programmatically in android?

后端 未结 19 2652
感动是毒
感动是毒 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:27

    Blundell's answer 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";
    }
    

提交回复
热议问题