Android雜談---擷取手機螢幕大小

來源:互聯網
上載者:User

 

Android雜談---擷取手機螢幕大小

SurfaceView簡單例子

玩轉Android---UI篇---ZoomControls放大縮小圖片

更多相關推薦 這裡總結下android中關於手機螢幕的相關操作:

1、擷取螢幕的解析度(因為android中處理顯示的時候,需要根據螢幕解析度的不同才去不同的布局或顯示不同的素材)

Java代碼 

// 通過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());

// 通過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());

2、去掉螢幕標題及全螢幕顯示

Java代碼 

// 去掉標題  

requestWindowFeature(Window.FEATURE_NO_TITLE);  

// 設定全屏  

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  

        WindowManager.LayoutParams.FLAG_FULLSCREEN);

// 去掉標題

requestWindowFeature(Window.FEATURE_NO_TITLE);

// 設定全屏

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

  WindowManager.LayoutParams.FLAG_FULLSCREEN);

3、設定螢幕的風向

在Manifest.xml檔案中配置Activity的屬性

Xml代碼 

<activity android:name=".AnimateActivity" android:label="@string/app_name" 

    android:screenOrientation="landscape"><!--landscape 橫屏portrait 豎屏--> 

    <intent-filter> 

        <action android:name="android.intent.action.MAIN" /> 

        <category android:name="android.intent.category.LAUNCHER" /> 

    </intent-filter> 

</activity>

<activity android:name=".AnimateActivity" android:label="@string/app_name"

 android:screenOrientation="landscape"><!--landscape 橫屏portrait 豎屏-->

 <intent-filter>

  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />

 </intent-filter>

</activity>

在程式中控制,一般在Activity中的onCreate、onDestroy方法中控制,因為在螢幕方向發生變化時,

系統會重新啟動Activity。所以需要再Activity銷毀前儲存相關資料,方便在下次onCreate方法中

重新載入,並更新螢幕的布局

Java代碼 

public void onCreate(Bundle savedInstanceState) {  

    //強制橫屏  

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  

    // TODO 更新螢幕布局  

}  

 

public void onDestroy() {  

    if(getRequestedOrientation() ==  

            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){  

        // 儲存資料  

    }else if(getRequestedOrientation() ==  

            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){  

        // 儲存資料  

    }  

聯繫我們

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