getting the screen density programmatically in android?

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

    To get 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;
    

提交回复
热议问题