android擷取裝置螢幕大小的方法

來源:互聯網
上載者:User

標籤:

// 通過WindowManager擷取DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);System.out.println("heigth : " + dm.heightPixels);System.out.println("width : " + dm.widthPixels);// 通過Resources擷取DisplayMetrics dm2 = getResources().getDisplayMetrics();System.out.println("heigth2 : " + dm2.heightPixels);System.out.println("width2 : " + dm2.widthPixels);// 擷取螢幕的預設解析度Display display = getWindowManager().getDefaultDisplay();System.out.println("width-display :" + display.getWidth());System.out.println("heigth-display :" + display.getHeight());

可以看到,第一、第三種方法都用到了getWindowManager()這個方法,而第二種方法沒有用getWindowManager()。有些網友說書寫getWindowManager()後eclipse會報錯,說沒有getWindowManager()這個方法,這是什麼原因呢?因為getWindowManager()這個方法是在類Activity中的,如果你自己編寫的類不是繼承於類Activity,那麼必然在這個類中書寫代碼就不能用到getWindowManager()這個方法。所以當你自己編寫的類不是繼承於類Activity,那麼就只能應用第二種方法擷取手機螢幕的大小。

android擷取裝置螢幕大小的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.