Comment verrouiller l’orientation pendant l’exécution

Est-il possible de verrouiller l’orientation pendant l’exécution? Par exemple, je voudrais permettre à l’utilisateur de verrouiller l’écran en mode paysage si l’utilisateur est actuellement en mode paysage et basculer l’option de menu.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

Appelé sur une activité, va le verrouiller au paysage. Recherchez les autres indicateurs dans la classe ActivityInfo. Vous pouvez le verrouiller en mode portrait ou le rendre piloté par un capteur ou un curseur.

Plus d’infos ici: http://www.devx.com/wireless/Article/40792

Faites attention à la différence entre ce que getConfiguration renvoie et ce que setRequestedOrientation veut – ils sont tous les deux int, mais ils proviennent de définitions de constantes différentes.

Voici comment verrouiller l’orientation actuelle tout en autorisant des renversements à 180 degrés

 int currentOrientation = getResources().getConfiguration().orientation; if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT); } 

Cela fonctionne sur les appareils avec portrait inversé et paysage inversé.

Orientation de locking:

  int orientation = getActivity().getRequestedOrientation(); int rotation = ((WindowManager) getActivity().getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); switch (rotation) { case Surface.ROTATION_0: orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; break; case Surface.ROTATION_90: orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; break; case Surface.ROTATION_180: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; break; default: orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; } getActivity().setRequestedOrientation(orientation); 

Débloquer l’orientation:

  getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 

J’ai semblé avoir eu un cas similaire. Je voulais soutenir n’importe quelle orientation, mais j’avais besoin de restr dans l’orientation actuelle après un certain point dans le stream de travail. Ma solution était:

A l’entrée du workflow protégé:

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); 

A la sortie du workflow protégé:

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 

Alternative à @pstoppani répond avec le support pour les tablettes (comme avec @pstoppani répondre, cela ne fonctionnera que sur les appareils> 2.2)
-Testé sur Samsung Galaxy SIII et Samsung Galaxy Tab 10.1

 public static void lockOrientation(Activity activity) { Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int rotation = display.getRotation(); int tempOrientation = activity.getResources().getConfiguration().orientation; int orientation = 0; switch(tempOrientation) { case Configuration.ORIENTATION_LANDSCAPE: if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; else orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; break; case Configuration.ORIENTATION_PORTRAIT: if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270) orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; else orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; } activity.setRequestedOrientation(orientation); } 

Voici mon code, vous pouvez verrouiller avec l’une de ces méthodes votre écran et une fois la tâche terminée, débloquez-la avec unlockOrientation:

 /** Static methods related to device orientation. */ public class OrientationUtils { private OrientationUtils() {} /** Locks the device window in landscape mode. */ public static void lockOrientationLandscape(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } /** Locks the device window in portrait mode. */ public static void lockOrientationPortrait(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } /** Locks the device window in actual screen mode. */ public static void lockOrientation(Activity activity) { final int orientation = activity.getResources().getConfiguration().orientation; final int rotation = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getRotation(); // Copied from Android docs, since we don't have these values in Froyo 2.2 int SCREEN_ORIENTATION_REVERSE_LANDSCAPE = 8; int SCREEN_ORIENTATION_REVERSE_PORTRAIT = 9; // Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO if (!BuildVersionUtils.hasGingerbread()) { SCREEN_ORIENTATION_REVERSE_LANDSCAPE = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; SCREEN_ORIENTATION_REVERSE_PORTRAIT = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; } if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90){ if (orientation == Configuration.ORIENTATION_PORTRAIT){ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if (orientation == Configuration.ORIENTATION_LANDSCAPE){ activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) { if (orientation == Configuration.ORIENTATION_PORTRAIT){ activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_PORTRAIT); } else if (orientation == Configuration.ORIENTATION_LANDSCAPE){ activity.setRequestedOrientation(SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } } } /** Unlocks the device window in user defined screen mode. */ public static void unlockOrientation(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER); } } 

Voici la conversion Xamarin de la réponse de @pstoppani ci-dessus.

NOTE: ceci est pour un fragment, remplacez l’ activité. avec ça si utilisé dans une activité.

 public void LockRotation() { ScreenOrientation orientation; var surfaceOrientation = Activity.WindowManager.DefaultDisplay.Rotation; switch (surfaceOrientation) { case SurfaceOrientation.Rotation0: orientation = ScreenOrientation.Portrait; break; case SurfaceOrientation.Rotation90: orientation = ScreenOrientation.Landscape; break; case SurfaceOrientation.Rotation180: orientation = ScreenOrientation.ReversePortrait; break; default: orientation = ScreenOrientation.ReverseLandscape; break; } Activity.RequestedOrientation = orientation; } public void UnlockRotation() { Activity.RequestedOrientation = ScreenOrientation.Unspecified; } 

Ceci n’a pas été testé avec une approche différente avant de l’utiliser, mais peut être utile.