簡體   English   中英

Android:手動屏幕方向而不重新啟動活動?

[英]Android: manual screen orientation without restarting the activity?

我需要制作一個應用程序播放帶有全屏視頻按鈕的視頻。 該按鈕用於在視頻顯示的橫向和縱向之間手動切換。 我們不希望自動旋轉檢測。 因此Manifest文件設置如下。

<activity
    android:name=".VideoActivity"
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden"/>

我用了

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

手動設置方向。 它工作但它重新啟動活動 - 發現onCreate()被調用。 因此,視頻播放意外地從頭開始重新開始。 我不能像使用onConfigurationChanged()一樣平滑 - 自動旋轉檢測方法。

那么如何在不重新啟動活動的情況下更改手動屏幕方向?

謝謝。

對於manaul方向更改:

<activity
    android:name=".VideoDetails"
    android:configChanges="orientation"/>

public class VideoDetails extends Activity {
    ...
    onStart() {
        setRequestedOrientation(orientation);
    }
    onConfigurationChanged(Configuration newConfig){
        // do stuff for orientation change.
    }
    onClick() {
        setRequestedOrientation(orientation);
    }
}

對於自動定向檢測:

<activity
    android:name=".VideoDetails"
    android:configChanges="orientation"/>

public class VideoDetails extends Activity {
    ...
    onConfigurationChanged(Configuration newConfig){
        // do stuff for orientation change.
    }
}

http://developer.android.com/guide/topics/resources/runtime-changes.html 您可以查看“ 處理配置更改自己 ”標題下的鏈接

<activity android:name=".MyActivity"
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

現在,當其中一個配置發生更改時,MyActivity不會重新啟動。 相反,MyActivity接收對onConfigurationChanged()的調用。 此方法傳遞一個Configuration對象,該對象指定新設備配置。

 android:configChanges="orientation|screenSize" (andorid 3.2 and above screen size also changes. add this)

假設您的視頻是10分鍾。 視頻播放到5分鍾,方向發生變化,你知道它已播放到5分鍾。

您可以在onSaveInstanceState()中保存視頻的實際進度,並從Bundle中獲取onRestoreInstanceState()中保存的數據,之后您可以使用進度數據開始播放,或者如果沒有保存數據則從頭開始播放。

當方向更改活動被銷毀並重新創建時。 如果要保存數據並保留,可以執行以下操作來保存大量數據

 @Override
 public Object onRetainNonConfigurationInstance() {
 final MyDataObject data = collectMyLoadedData();
 return data;
 }


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
    data = loadMyData();
}

}

對於小數據集

 @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {
 super.onSaveInstanceState(savedInstanceState);
 // Save UI state changes to the savedInstanceState.
 // This bundle will be passed to onCreate if the process is
 // killed and restarted.
 savedInstanceState.putString("NICK_NAME", Name);//save nick name
 }

 @Override
 public void onRestoreInstanceState(Bundle savedInstanceState) {
 super.onRestoreInstanceState(savedInstanceState);
 // Restore UI state from the savedInstanceState.
 // This bundle has also been passed to onCreate.
 Name = savedInstanceState.getString("NICK_NAME");//restore nick name
 }

檢查方向

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}

Android中的VideoView 在這種情況下,視頻也從服務器流式傳輸。 檢查接受的答案(commonsware答案)。 我建議完全一樣。

首先將android:configChanges節點添加到Activity's manifest節點

android:configChanges="keyboardHidden|orientation"

有關更多信息,請查看鏈接

添加標簽

"android:configChanges="keyboard|keyboardHidden|orientation|screenSize|navigation" for your activity in Manifest.xml,

並覆蓋該方法

public void onConfigurationChanged(Configuration newConfig) {}

如果您需要在orientationchange chek 上更改布局,則可以在方向更改時替換布局

在方向更改后重新啟動應用程序是不可避免的。

保持與視頻狀態相關的變量在應用程序上,以便活動不會在開始時重新開始播放。

此代碼來自我為測試此答案而制作的示例應用程序。

public class MyApplication extends Application {
int counter;
@Override
public void onCreate() {
    super.onCreate();
    counter = 0;
    }
}   

在清單上添加應用程序

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    android:name="com.example.orientationchange.MyApplication" >
    <activity
 ...

使用活動:

public class MainActivity extends Activity {

TextView output;
MyApplication app ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    app = (MyApplication) getApplication();
    setContentView(R.layout.activity_main);
    output = (TextView) findViewById(R.id.output);
    //A counter thread used to test the solution.
    new CounterTask().execute( 100 );
}
 ...

現在旋轉不會清除狀態變量。

您也可以使用“首選項”來獲取更多代碼密集型解決方案來保留狀態。 首選項還將記住從一次使用應用程序到另一次應用程序的狀態,其中應用程序狀態被清除應用程序的onDestroy()。

我一直在尋找解決方案,對我來說,在Manifest中將方向定義為鎖定

<activity
    android:name=".VideoActivity"
    android:screenOrientation="locked"
    android:configChanges="keyboardHidden"/>

即使您調用setRequestedOrientation() ,也可以防止重新創建Activity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM