vertical studio solo rotar rotacion pantalla orientacion horizontal forzar fijar como bloquear aplicacion android locking rotation screen orientation

solo - fijar orientacion pantalla android studio



Bloqueo de orientación de pantalla (8)

¿Hay alguna manera confiable de bloquear la orientación de la pantalla en todos los dispositivos Android? El siguiente código funciona para mi Nexus S y otros teléfonos, pero por alguna razón ROTATION_90 corresponde a SCREEN_ORIENTATION_REVERSE_PORTRAIT en el Xoom.

¿Hay alguna manera de asignar de manera confiable la rotación a la orientación?

private void lockScreenOrientation() { if (!mScreenOrientationLocked) { final int orientation = getResources().getConfiguration().orientation; final int rotation = getWindowManager().getDefaultDisplay().getOrientation(); if (rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_90) { if (orientation == Configuration.ORIENTATION_PORTRAIT) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } } else if (rotation == Surface.ROTATION_180 || rotation == Surface.ROTATION_270) { if (orientation == Configuration.ORIENTATION_PORTRAIT) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } } mScreenOrientationLocked = true; } } private void unlockScreenOrientation() { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); mScreenOrientationLocked = false; }

EDITAR: Este código está destinado a obtener la orientación actual y bloquearla. La orientación se bloquea temporalmente y luego se libera al usuario.


Aquí está mi solución, funciona en teléfonos y tabletas en cualquier Android SDK.

switch (getResources().getConfiguration().orientation){ case Configuration.ORIENTATION_PORTRAIT: if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else { int rotation = getWindowManager().getDefaultDisplay().getRotation(); if(rotation == android.view.Surface.ROTATION_90|| rotation == android.view.Surface.ROTATION_180){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } break; case Configuration.ORIENTATION_LANDSCAPE: if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.FROYO){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { int rotation = getWindowManager().getDefaultDisplay().getRotation(); if(rotation == android.view.Surface.ROTATION_0 || rotation == android.view.Surface.ROTATION_90){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } } break; }


Esta solución solo se basa en otros. Es una forma diferente de abordar el problema del enlightenment ya abordado: en algunos dispositivos, landscape es ROTATION_90 , pero en (algunos) otros es ROTATION_270 . Cuando probé algo así como la solución de enl8enmentnow en un Kindle Fire HD 7 ", hizo que la pantalla girara hacia arriba y luego inmediatamente hacia atrás. No he visto más ideas que codificar qué dispositivos consideran el paisaje como 270, así que aquí está solución hard-coded:

public static void unlockOrientation() { activity.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); } public static void lockOrientation() { if (Build.VERSION.SDK_INT < 18) { activity.setRequestedOrientation(getOrientation()); } else { activity.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LOCKED); } } private static int getOrientation() { int port = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; int revP = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; int land = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; int revL = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; if (Build.VERSION.SDK_INT < 9) { revL = land; revP = port; } else if (isLandscape270()) { land = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; revL = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } Display display = activity.getWindowManager().getDefaultDisplay(); boolean wide = activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; switch (display.getRotation()) { case Surface.ROTATION_0: return wide ? land : port; case Surface.ROTATION_90: return wide ? land : revP; case Surface.ROTATION_180: return wide ? revL : revP; case Surface.ROTATION_270: return wide ? revL : port; default: throw new AssertionError(); } } private static boolean isLandscape270() { return android.os.Build.MANUFACTURER.equals("Amazon") && !(android.os.Build.MODEL.equals("KFOT") || android.os.Build.MODEL.equals("Kindle Fire")); }

isLandscape270() detecta si el dispositivo es un Kindle de segunda generación o posterior (consulte este enlace para obtener el MODEL de este enlace ). No sé si otros dispositivos también deberían incluirse; por favor comente si sabe de alguno.

Además, en las API> = 18 esto simplemente usa setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED) . Solo he probado eso en emuladores; por favor comente si tiene problemas en dispositivos reales.


Mi solución:

int orientation=act.getResources().getConfiguration().orientation; int rotation=act.getWindowManager().getDefaultDisplay().getOrientation(); if (orientation==Configuration.ORIENTATION_PORTRAIT) {if (rotation==Surface.ROTATION_0 || rotation==Surface.ROTATION_270) //0 for phone, 270 for tablet {act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else {act.setRequestedOrientation(9);//instead of SCREEN_ORIENTATION_REVERSE_PORTRAIT when <= android 2.2 } } else {if (rotation==Surface.ROTATION_90 || rotation==Surface.ROTATION_0) //90 for phone, 0 for tablet {act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else {act.setRequestedOrientation(8);//instead of SCREEN_ORIENTATION_REVERSE_LANDSCAPE when <= android 2.2 } }


Modifiqué ligeramente las respuestas del diyismo para compensar el hecho de que no puedes usar los modos reverse_landscape y reverse_portrait antes de la versión 2.3

private static void disableRotation(Activity activity) { final int orientation = activity.getResources().getConfiguration().orientation; final int rotation = activity.getWindowManager().getDefaultDisplay().getOrientation(); // 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; if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.FROYO) { 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); } } } private static void enableRotation(Activity activity) { activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); }



Use: setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR) porque la orientación está determinada por un sensor de orientación física: la pantalla girará en función de cómo el usuario mueva el dispositivo. Esto permite cualquiera de las 4 rotaciones posibles, independientemente de lo que normalmente hará el dispositivo (por ejemplo, algunos dispositivos normalmente no usarán una rotación de 180 grados). Y tu código debería funcionar en Xoom también ...


para un bloqueo de pantalla temporal que puede usar fácilmente:

//developing for android tablets **<uses-sdk android:minSdkVersion="12" />** //works perfectly... **WATCH OUT**: look portrait to reverse-portrait on api level 13 :) currentActivity.setRequestedOrientation(currentActivity.getResources().getConfiguration().orientation); //to re-enable sensor, just do: currentActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);

lo usé para un bloqueo de pantalla temporal durante la presentación del diálogo y haciendo un importante trabajo de fondo.

asegúrese de que la actividad actual sea válida en el momento en que intente acceder a ella, de lo contrario no funcionará :)

buena suerte :)


// Works on all devices. The other solution only works on 1/2 of the devices. // Lock orientation int rotation = getWindowManager().getDefaultDisplay().getRotation(); lockOrientation(rotation, Surface.ROTATION_270); // Ensure that the rotation hasn''t changed if (getWindowManager().getDefaultDisplay().getRotation() != rotation) { lockOrientation(rotation, Surface.ROTATION_90); } // ... private void lockOrientation(int originalRotation, int naturalOppositeRotation) { int orientation = getResources().getConfiguration().orientation; if (orientation == Configuration.ORIENTATION_PORTRAIT) { // Are we reverse? if (originalRotation == Surface.ROTATION_0 || originalRotation == naturalOppositeRotation) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } else { setReversePortrait(); } } else { // Are we reverse? if (originalRotation == Surface.ROTATION_0 || originalRotation == naturalOppositeRotation) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { setReverseLandscape(); } } } @SuppressLint("InlinedApi") private void setReversePortrait() { if (Build.VERSION.SDK_INT >= 9) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } @SuppressLint("InlinedApi") private void setReverseLandscape() { if (Build.VERSION.SDK_INT >= 9) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } }