android 動態改變螢幕方向

來源:互聯網
上載者:User

標籤:android   class   code   java   http   ext   

LANDSCAPE與PORTRAIT

範例說明

要如何通過程式控制Activity的顯示方向?在Android中,若要通過程式改變螢幕顯示的方向,必須要覆蓋 setRequestedOrientation()方法,而若要取得目前的螢幕方向,則需要訪問getRequestedOrientation()方 法。

本 範例為求簡要示範更改做法,設計了一個按鈕,當單擊按鈕的同時,判斷當下的螢幕方向,例如豎排(PORTRAIT),則將其更改為橫排 (LANDSCAPE);若為橫排(LANDSCAPE),則將其更改為豎排(PORTRAIT),範例非常簡單。圖5-25所示是啟動並執行結果。

本程式重寫setRequestedOrientation()方法,其目的是為了要捕捉設定螢幕方向的同時所觸發的事件,並在更改的時候,以Toast顯示要更改的方向。

範常式序

src/irdc.ex05_22/EX05_22.java

程 序一開始(onCreate)先判斷getRequestedOrientation()的值是否為-1,若此值為-1,表示在Activity屬性裡沒 有設定Android:screenOrientation的值,這意味著即使單擊按鈕,也無法判斷螢幕的方向,不會變更方向的事件了。

在 被覆蓋的setRequestedOrientation()事件裡,會傳入要轉換的方向常數(requestedOrientation),其值為整數 類型,有以SCREEN_ORIENTATION_PORTRAIT及SCREEN_ORIENTATION_LAN- DSCAPE兩個指定常數。

/* import程式略 */

import android.content.pm.ActivityInfo;

import android.view.Display;

public class EX05_22 extends Activity

{

private TextView mTextView01;

private Button mButton01;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mButton01 = (Button)findViewById(R.id.myButton1);

mTextView01 = (TextView)findViewById(R.id.myTextView1);

if(getRequestedOrientation()==-1)

{

mTextView01.setText(getResources().getText

(R.string.str_err_1001));

}

/* 當單擊按鈕旋轉螢幕畫面 */

mButton01.setOnClickListener(new Button.OnClickListener()

{

@Override

public void onClick(View arg0)

{

/* 方法一:重寫getRequestedOrientation */

/* 若無法取得screenOrientation屬性 */

if(getRequestedOrientation()==-1)

{

/* 提示無法進行畫面旋轉功能,因無法判別Orientation */

mTextView01.setText(getResources().getText

(R.string.str_err_1001));

}

else

{

if(getRequestedOrientation()==

ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

{

/* 若目前為橫排,則更改為豎排呈現 */

setRequestedOrientation

(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

else if(getRequestedOrientation()==

ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

{

/* 若目前為豎排,則更改為橫排呈現 */

setRequestedOrientation

(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}

}

}

});

}

@Override

public void setRequestedOrientation(int requestedOrientation)

{

// TODO Auto-generated method stub

/* 判斷要更改的方向,以Toast提示 */

switch(requestedOrientation)

{

/* 更改為LANDSCAPE */

case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE):

mMakeTextToast

(

getResources().getText(R.string.str_msg1).toString(),

false

);

break;

/* 更改為PORTRAIT */

case (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT):

mMakeTextToast

(

getResources().getText(R.string.str_msg2).toString(),

false

);

break;

}

super.setRequestedOrientation(requestedOrientation);

}

@Override

public int getRequestedOrientation()

{

// TODO Auto-generated method stub

/* 此重寫getRequestedOrientation方法,可取得目前螢幕的方向 */

return super.getRequestedOrientation();

}

public void mMakeTextToast(String str, boolean isLong)

{

if(isLong==true)

{

Toast.makeText(EX05_22.this, str, Toast.LENGTH_LONG).show();

}

else

{

Toast.makeText(EX05_22.this, str, Toast.LENGTH_SHORT).show();

}

}

}

AndroidManifest.xml

請留意在AndroidManifest.xml當中需要設定Activity的Android:screenOrientation屬性,否則,程式將無法通過getRequestedOrientation()方法,來判斷現在Activity的方向。

 

<manifest< p="">

xmlns:android="http://schemas.android.com/apk/res/android"

package="irdc.ex05_22"

android:versionCode="1"

android:versionName="1.0.0">

<application< p="">

android:icon="@drawable/icon"

android:label="@string/app_name">

<activity< p="">

android:name=".EX05_22"

android:label="@string/app_name"

android:screenOrientation="portrait">

 

 

 

 

 

 

 

 

擴充學習

在上面的程式裡,是以調用getRequestedOrientation()方法來判斷單擊按鈕時,螢幕的顯示方向雖然程式也可以進行判斷,但以下方法可以適用在長寬比不一樣的手機上。

/* 方法二:判斷螢幕寬高比 */

final Display defaultDisplay =

getWindow().getWindowManager().getDefaultDisplay();

int h= defaultDisplay.getHeight();

int w = defaultDisplay.getWidth();

/* 此解析度為按鈕單擊當下的解析度 */

mTextView01.setText

(Integer.toString(h)+"x"+Integer.toString(w));

/if(w > h)

{

/* Landscape */

/* 重寫Activity裡的setRequestedOrientation()方法 */

setRequestedOrientation

(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

else

{

/* Portrait */

setRequestedOrientation

(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}

聯繫我們

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