Android 擷取螢幕尺寸與密度

來源:互聯網
上載者:User

android中擷取螢幕的長於寬,參考了網上有很多代碼,但結果與實際不符,如My Phone是i9000,螢幕大小是480*800px,得到的結果卻為320*533

結果很不靠譜,於是自己寫了幾行代碼,親測一下

測試參數:

測試環境: i9000(三星)

物理螢幕:480*800px

density :1.5

測試代碼:

// 擷取螢幕密度(方法1)<br /> int screenWidth = getWindowManager().getDefaultDisplay().getWidth();// 螢幕寬(像素,如:480px)<br /> int screenHeight = getWindowManager().getDefaultDisplay().getHeight();// 螢幕高(像素,如:800p)</p><p> Log.e(TAG + " getDefaultDisplay", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);</p><p> // 擷取螢幕密度(方法2)<br /> DisplayMetrics dm = new DisplayMetrics();<br /> dm = getResources().getDisplayMetrics();</p><p> float density = dm.density;// 螢幕密度(像素比例:0.75/1.0/1.5/2.0)<br /> int densityDPI = dm.densityDpi;// 螢幕密度(每寸像素:120/160/240/320)<br /> float xdpi = dm.xdpi;<br /> float ydpi = dm.ydpi;</p><p> Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);<br /> Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);</p><p> screenWidth = dm.widthPixels;// 螢幕寬(像素,如:480px)<br /> screenHeight = dm.heightPixels;// 螢幕高(像素,如:800px)</p><p> Log.e(TAG + " DisplayMetrics(111)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);</p><p> // 擷取螢幕密度(方法3)<br /> dm = new DisplayMetrics();<br /> getWindowManager().getDefaultDisplay().getMetrics(dm);</p><p> density = dm.density;// 螢幕密度(像素比例:0.75/1.0/1.5/2.0)<br /> densityDPI = dm.densityDpi;// 螢幕密度(每寸像素:120/160/240/320)<br /> xdpi = dm.xdpi;<br /> ydpi = dm.ydpi;</p><p> Log.e(TAG + " DisplayMetrics", "xdpi=" + xdpi + "; ydpi=" + ydpi);<br /> Log.e(TAG + " DisplayMetrics", "density=" + density + "; densityDPI=" + densityDPI);</p><p> int screenWidthDip = dm.widthPixels;// 螢幕寬(dip,如:320dip)<br /> int screenHeightDip = dm.heightPixels;// 螢幕寬(dip,如:533dip)</p><p> Log.e(TAG + " DisplayMetrics(222)", "screenWidthDip=" + screenWidthDip + "; screenHeightDip=" + screenHeightDip);</p><p> screenWidth = (int)(dm.widthPixels * density + 0.5f);// 螢幕寬(px,如:480px)<br /> screenHeight = (int)(dm.heightPixels * density + 0.5f);// 螢幕高(px,如:800px)</p><p> Log.e(TAG + " DisplayMetrics(222)", "screenWidth=" + screenWidth + "; screenHeight=" + screenHeight);
結果如下:

E/== MyScreenActivity =================================== getDefaultDisplay( 8509): screenWidth=320; screenHeight=533<br />E/== MyScreenActivity =================================== DisplayMetrics( 8509): xdpi=156.3077; ydpi=157.51938<br />E/== MyScreenActivity =================================== DisplayMetrics( 8509): density=1.0; densityDPI=160<br />E/== MyScreenActivity =================================== DisplayMetrics(111)( 8509): screenWidth=320; screenHeight=533<br />E/== MyScreenActivity =================================== DisplayMetrics( 8509): xdpi=234.46153; ydpi=236.27907<br />E/== MyScreenActivity =================================== DisplayMetrics( 8509): density=1.5; densityDPI=240<br />E/== MyScreenActivity =================================== DisplayMetrics(222)( 8509): screenWidthDip=320; screenHeightDip=533<br />E/== MyScreenActivity =================================== DisplayMetrics(222)( 8509): screenWidth=480; screenHeight=800

分析結果:

在onDraw()方法中

方法1和2,得到的結果都一致,均為320*533,明顯不是測試機i9000的螢幕大小

方法3,將方法1和2得到的結果,乘以density後,完美的480*800,perfect!

註: density 大於1的情況下,需要設定targetSdkVersion在4-9之間,例如
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="10" />


仔細分析代碼,發現問題出在代碼:

getWindowManager().getDefaultDisplay().getMetrics(dm)

Initialize a DisplayMetrics object from this display's data.

dm = getResources().getDisplayMetrics()

Return the current display metrics that are in effect for this resource object. The returned object should be treated as read-only.


附:測試源碼


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.