getting the screen density programmatically in android?

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

    The following answer is a small improvement based upon qwertzguy's answer.

    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
    }
    

提交回复
热议问题