vertical studio screenorientation rotación pantalla orientación orientacion manejando horizontal detectar cambio bloquear android orientation

screenorientation - orientación horizontal android studio



Cómo bloquear la orientación durante el tiempo de ejecución (7)

Alternativa a la respuesta de @pstoppani con soporte para tabletas (Al igual que con la respuesta de @pstoppani, esto solo funcionará en dispositivos> 2.2)
-Estado en Samsung Galaxy SIII y 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); }

¿Hay alguna manera de bloquear la orientación durante el tiempo de ejecución? Por ejemplo, me gustaría permitir al usuario bloquear la pantalla en el paisaje si el usuario está actualmente en el paisaje y alternar la opción del menú.


Aquí está la conversión de Xamarin de la respuesta de @pstoppani anterior.

NOTA: esto es para un Fragmento, reemplazar Actividad. con esto si se usa dentro de una actividad.

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; }

Esto no se ha probado como fue con un enfoque diferente antes de usarlo, pero puede ser útil.


Aquí está mi código, puedes bloquear con uno de estos métodos tu pantalla y una vez que hayas terminado la tarea desbloquéalo con desbloquearOrientación:

/** 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); } }


Esto funciona en dispositivos con reverso y paisaje inverso.

Orientación de bloqueo:

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);

Desbloquear orientación:

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);


Parecía tener un caso similar. Quería apoyar cualquier orientación, pero necesitaba permanecer en la orientación actual después de cierto punto en el flujo de trabajo. Mi solución fue:

En la entrada del flujo de trabajo protegido:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);

Al salir del flujo de trabajo protegido:

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);


Tenga cuidado con la diferencia entre lo que getConfiguration devuelve y lo que quiere setRequestedOrientation: ambos son int, pero provienen de diferentes definiciones de constantes.

A continuación, se explica cómo bloquear la orientación actual, a la vez que permite volteos de 180 grados

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


setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Invocado en una actividad, lo bloqueará en el paisaje. Busque los otros indicadores en la clase ActivityInfo. Puede bloquearlo de nuevo en vertical o convertirlo en sensor / control deslizante.

Más información aquí: http://www.devx.com/wireless/Article/40792