當前位置:首頁 » 安卓系統 » androidmobile

androidmobile

發布時間: 2023-03-01 13:19:23

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造成的卡頓與短暫的黑屏切換。

熱點內容
汽修汽配源碼 發布:2025-05-14 20:08:53 瀏覽:742
蜜蜂編程官網 發布:2025-05-14 19:59:28 瀏覽:57
優酷怎麼給視頻加密 發布:2025-05-14 19:31:34 瀏覽:635
夢三國2副本腳本 發布:2025-05-14 19:29:58 瀏覽:860
phpxmlhttp 發布:2025-05-14 19:29:58 瀏覽:434
Pua腳本 發布:2025-05-14 19:24:56 瀏覽:449
蘋果像素低為什麼比安卓好 發布:2025-05-14 19:13:23 瀏覽:461
安卓機微信怎麼設置紅包提醒 發布:2025-05-14 19:00:15 瀏覽:272
androidsystem許可權設置 發布:2025-05-14 18:56:02 瀏覽:971
mq腳本 發布:2025-05-14 18:45:37 瀏覽:25