關於Android屏幕方面

參考幾個網頁連接: html

http://www.2cto.com//kf/201402/280704.html android

http://blog.csdn.net/moruite/article/details/7281428 測試

http://zhidao.baidu.com/link?url=l24PwUM0aq40fD6gzDBaSGYeJURtp2L8yvVQRQmxDWWdLHkFIDJqLrNhpCBIrgXEmzCQ-JylHsDGAvAwW-fkRBSBzhs3chA03wqJ-sah437 ui

dp(dip):設備獨立像素 url

dpi:每英寸的像素個數(120/ 160/ 240/ 320) spa

density:密度 ( 0.75/ 1.0/ 1.5/ 2.0) .net

px=dp*(dpi/160) orm

屏幕尺寸=屏幕對角線的像素值/(密度*160)=   / (密度*160) xml

須要在工程的AndroidManifest.xml文件中,加入supports-screens節點,以下: <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:resizeable="true" android:anyDensity="true" /> 這樣當前的Android程序就支持了多種分辨率,那麼就能夠獲得正確的物理尺寸 htm

 

DisplayMetrics metric = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(metric);

int width = metric.widthPixels; // 寬度(PX)

int height = metric.heightPixels; // 高度(PX)

float density = metric.density; // 密度(0.75 / 1.0 / 1.5)

int densityDpi = metric.densityDpi; // 密度DPI(120 / 160 / 240)

 

實踐過程:

因爲Android設備中獲取的密度density自己是個約等於的數值,好比計算出密度的準確值density=1.575,實際在代碼中讀到的density=1.5, 須要實現一個簡單的計算屏幕尺寸的應用在不一樣設備上驗證物理尺寸準確程度,具體代碼以下:

實踐結果:幾個不一樣設備(手機和平板)的測試結果以下:

相關文章
相關標籤/搜索