2016-09-20 89 views
0

如何以編程方式獲取設備DPI。我需要的價值並不像華電國際和MDPI如何以編程方式獲取設備DPI?

設備
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; 
} 

這個代碼僅顯示高或低,但我需要的價值

+1

的可能的複製[獲取屏幕密度編程在機器人?](http://stackoverflow.com/questions/3166501/getting-the-screen-density-programmatically-in-android) –

回答

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; 
+0

PPI和DPI都相同? –

0

使用metrics.xdpimetrics.ydpi

+0

PPI和DPI兩者是相同的? –

+0

- DPI是指包含在一英寸圖像內的打印點數。 - PPI是指包含在一英寸圖像內的像素數量。 –

相關問題