How to check an Android device is HDPI screen or MDPI screen?

前端 未结 7 2174
你的背包
你的背包 2020-11-27 09:27

I want to check this to fetch different images by internet. How to do that?

7条回答
  •  抹茶落季
    2020-11-27 10:16

    You can check the screen density with:

    switch (getResources().getDisplayMetrics().densityDpi) {
    case DisplayMetrics.DENSITY_LOW:
        // ...
        break;
    case DisplayMetrics.DENSITY_MEDIUM:
        // ...
        break;
    case DisplayMetrics.DENSITY_HIGH:
        // ...
        break;
    case DisplayMetrics.DENSITY_XHIGH:
        // ...
        break;
    }
    

    EDIT Be aware that as Android evolves, other values should be included in the switch cases. As of this edit, this includes DisplayMetrics.DENSITY_TV and DisplayMetrics.DENSITY_XXHIGH. Consult the docs for the latest info; I'm not going to bother maintaining this answer.

提交回复
热议问题