如何以编程方式获取Device DPI?

时间:2016-09-20 08:48:54

标签: java android hdpi

如何以编程方式获取Device DPI。我需要的值不是HDPI或MDPI等设备

    DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
     case DisplayMetrics.DENSITY_LOW:
                break;
     case DisplayMetrics.DENSITY_MEDIUM:
                 break;
     case DisplayMetrics.DENSITY_HIGH:
                 break;
}

此代码仅显示高或低但我需要值

2 个答案:

答案 0 :(得分:1)

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;

答案 1 :(得分:0)

使用metrics.xdpimetrics.ydpi