androidmobile
1. 如何讓安卓手機判定屏幕旋轉90度 how to make android mobile phone to determine
在介紹之前,我們需要先了解默認情況下android屏幕旋轉的機制:
默認情況下,當用戶手機的重力感應器打開後,旋轉屏幕方向,會導致當前activity發生onDestroy-> onCreate,這樣會重新構造當前activity和界面布局,如果在Camera界面,則表現為卡頓或者黑屏一段時間。如果是在橫豎屏UI設計方面,那麼想很好地支持屏幕旋轉,則建議在res中建立layout-land和layout-port兩個文件夾,把橫屏和豎屏的布局文件分別放入對應的layout文件夾中。
了解了這些以後,我們對android的屏幕旋轉方法進行如下總結:
1. AndroidManifest.xml設置
如果單單想設置橫屏或者豎屏,那麼只需要添加橫豎屏代碼:
android:screenOrientation="landscape"橫屏設置;
android:screenOrientation="portrait"豎屏設置;
這種方法的優點:即使屏幕旋轉,Activity也不會重新onCreate。
缺點:屏幕只有一個方向。
2. 代碼動態設置
如果你需要動態改變橫豎屏設置,那麼,只需要在代碼中調用setRequestedOrientation()函數:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//橫屏設置
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//豎屏設置
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
//默認設置
這種方法優點:可以隨意動態設置,滿足我們人為改變橫豎屏的要求,同時滿足橫豎屏UI不同的設計需求;
缺點:如果改變設置,那麼,Activity會被銷毀,重新構建,即重新onCreate;
3. 重寫onConfigurationChanged
如果你不希望旋轉屏幕的時候Activity被不斷的onCreate(這種情況往往會造成屏幕切換時的卡頓),那麼,可以使用此方法:
首先,在AndroidMainfest.xml中添加configChanges:
<activity android:name=".Test"
android:configChanges="orientation|keyboard">
</activity>
注意,keyboardHidden表示鍵盤輔助功能隱藏,如果你的開發API等級等於或高於13,還需要設置screenSize,因為screenSize會在屏幕旋轉時改變;
android:configChanges="keyboardHidden|orientation|screenSize"
然後,在Activity中重寫onConfigurationChanged方法,這個方法將會在屏幕旋轉變化時,進行監聽處理:
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stubsuper.onConfigurationChanged(newConfig);
if (newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
// Nothing need to be done here
} else {
// Nothing need to be done here
}
}
這個方法的優點:我們可以隨時監聽屏幕旋轉變化,並對應做出相應的操作;
缺點:它只能一次旋轉90度,如果一下子旋轉180度,onConfigurationChanged函數不會被調用。
4. 結合OrientationEventListener,自定義旋轉監聽設置
如果你想更加完美,更加完全的掌控監聽屏幕旋轉變化,比如,轉屏時不想重新onCreate,尤其是在Camera界面,不想出現旋轉preview時屏幕的卡頓、黑屏等問題,那麼,可以嘗試:
首先,創建OrientationEventListener對象:
private OrientationEventListener mOrientationListener;
// screen orientation listener
private boolean mScreenProtrait = true;
private boolean mCurrentOrient = false;
然後,自定義屏幕變化回調介面
abstract protected void OrientationChanged(int orientation);
//screen orientation change event
最後,自定義監聽類
private final void () {
mOrientationListener = new OrientationEventListener(this) {
@Override
public void onOrientationChanged(int rotation) {
if (((rotation >= 0) && (rotation <= 45)) || (rotation >= 315)||((rotation>=135)&&(rotation<=225))) {//portrait
mCurrentOrient = true;
if(mCurrentOrient!=mScreenProtrait)
{
mScreenProtrait = mCurrentOrient;
OrientationChanged(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Log.d(TAG, "Screen orientation changed from Landscape to Portrait!");
}
}
else if (((rotation > 45) && (rotation < 135))||((rotation>225)&&(rotation<315))) {//landscape
mCurrentOrient = false;
if(mCurrentOrient!=mScreenProtrait)
{
mScreenProtrait = mCurrentOrient;
OrientationChanged(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Log.d(TAG, "Screen orientation changed from Portrait to Landscape!");
}
}
}
};
mOrientationListener.enable();
}
在onCreate()中調用:
();
這個方法的優點:你可以任意隨時准確的監聽屏幕旋轉變化的狀態,可以隨時動態改變橫豎屏狀態;
註:對於Camera來說,你可以設置初始化為橫屏或者豎屏,然後對外提供旋轉監聽,這樣,既可以獲得屏幕旋轉狀態,讓你做出相應的操作,又不會出現重新onCreate當前Activity造成的卡頓與短暫的黑屏切換。