我有以下代码。
爪哇
public void lockScreenOrientation() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
}
public void unlockScreenOrientation() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}
我从javascript调用这些函数。控制正在进入这些方法。但是方向没有锁定。
我试过按下以锁定方向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
这些似乎都不起作用。任何指针都会有所帮助。
答案 0 :(得分:5)
我已经创建了一些实用程序方法来帮助处理方向锁定,可以随意使用这个类。
使用示例:
OrientationUtils.lockOrientationPortrait(MyActivityName.this)
OrientationUtils.lockOrientationLandscape(getActivity())
代码:
/** 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);
}
/** Allows user to freely use portrait or landscape mode. */
public static void unlockOrientation(Activity activity) {
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
这是我在GitHub上的完整OrientationUtils类,可以在任何Android应用中使用:https://github.com/danialgoodwin/android-simply-advanced-helper/blob/master/SimplyAdvancedHelperLibrary/src/net/simplyadvanced/utils/OrientationUtils.java
答案 1 :(得分:4)
Activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
以任何方向锁定屏幕(活动)。
要求API级别> = 18
答案 2 :(得分:1)
这是我编写的用于处理锁定和解锁屏幕方向的类。我从切换按钮的checkedChangedListener中调用toggleScreenOrientationLock(this, prefs, isChecked)
,从onCreate()调用restoreScreenLock(this, prefs)
。在这两种情况下,this
都是您的活动,prefs
是SharedPrefences
对象,用于保存有关锁定状态的信息。
代码的复杂部分是getScreenOrientation()
函数which I stole from and cleaned up from here。我将尝试解释其运作方式背后的逻辑。
当我们使用setRequestedOrienation()
设置设备的方向时,我们需要知道设备是处于横向还是纵向模式,我们需要知道它是否是反向(旋转180°)度)。
使用getResources().getConfiguration().orientation
将回答我们处于哪个方向的问题。如果我们可以考虑设备的旋转,我们可以判断它是否旋转了180。不幸的是,根据设备的不同,ROTATE_0可能是纵向或横向。手机通常将ROTATE_0映射到纵向,将平板电脑映射到横向。
因此,此处使用的解决方案是使用屏幕尺寸来确定它是横向还是纵向。如果屏幕宽度超过它的高度,那么我们推断设备处于横向方向,反之亦然。然后我们可以考虑旋转来确定方向是否反转。
例如,如果屏幕宽度超过它的高度,那么我们知道我们处于横向方向。如果旋转为0或180(在代码的逻辑中,这等于!isRotatedOrthogonally),那么我们知道0是LANDSCAPE,180是REVERSE_LANDSCAPE。
It has been noted elsewhere这不会在所有设备上运行,因为90或270是否是反向定位是设备特定的。但这仍然是你要做的最好的事情;在最坏的情况下,当你锁定它时,一个方向将旋转180度,如果试图以任何其他方式锁定屏幕,这可能会发生。
public class ScreenLocker {
final private static String ROTATION_LOCKED_KEY = "LockedOrientationVal";
final private static String ROTATION_IS_LOCKED_KEY = "IsRotationLocked";
final private static String ROTATION_SAVED_KEY = "SavedOrientationVal";
public static int getScreenOrientation(Activity activity) {
final Display display = activity.getWindowManager().getDefaultDisplay();
final int rotation = display.getRotation();
Point size = new Point();
display.getSize(size);
final boolean isWiderThanTall = size.x > size.y;
final boolean isRotatedOrthogonally = (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270);
int orientation;
if (isRotatedOrthogonally) {
if (isWiderThanTall)
orientation = (rotation == Surface.ROTATION_90) ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
else
orientation = (rotation == Surface.ROTATION_90) ? ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; // normal and reversed switched intended
}
else {
if (isWiderThanTall)
orientation = (rotation == Surface.ROTATION_0) ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE : ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
else
orientation = (rotation == Surface.ROTATION_0) ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
return orientation;
}
public static void toggleScreenOrientationLock(Activity activity,SharedPreferences prefs, boolean lock) {
if(lock)
lockScreenOrientation(activity, prefs);
else
unlockScreenOrientation(activity, prefs);
}
// call this from your activity's onCreate() or onResume()
public static boolean restoreScreenLock(Activity activity, SharedPreferences prefs) {
final boolean isLocked = prefs.getBoolean(ROTATION_IS_LOCKED_KEY, false);
final int previousLockedOrientation = prefs.getInt(ROTATION_LOCKED_KEY, -999);
if(isLocked && previousLockedOrientation != -999) {
prefs.edit().putInt(ROTATION_SAVED_KEY, activity.getRequestedOrientation()).apply();
activity.setRequestedOrientation(previousLockedOrientation);
return true;
}
return false;
}
private static void lockScreenOrientation(Activity activity, SharedPreferences prefs) {
final int currentOrientation = activity.getRequestedOrientation();
final int lockOrientation = getScreenOrientation(activity);
// checking isCurrentlyLocked prevents the ROTATION_LOCKED_KEY and ROTATION_SAVED_KEY
// becoming identical, which results in the screen not being able to be unlocked.
final boolean isCurrentlyLocked = prefs.getBoolean(ROTATION_IS_LOCKED_KEY, false);
if(!isCurrentlyLocked) {
activity.setRequestedOrientation(lockOrientation);
prefs.edit()
.putInt(ROTATION_SAVED_KEY, currentOrientation)
.putInt(ROTATION_LOCKED_KEY, lockOrientation)
.putBoolean(ROTATION_IS_LOCKED_KEY, true)
.apply();
}
}
private static void unlockScreenOrientation(Activity activity, SharedPreferences prefs) {
final int savedOrientation = prefs.getInt(ROTATION_SAVED_KEY, activity.getRequestedOrientation());
activity.setRequestedOrientation(savedOrientation);
prefs.edit().putBoolean(ROTATION_IS_LOCKED_KEY, false).apply();
}
}
答案 3 :(得分:0)
这是另一个适合我的简单解决方案。
private void orientationManager(boolean lock)
{
int currentOrientation = getResources().getConfiguration().orientation;
if(lock)
{
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE)
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
}
else
{
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
}
我需要在打开对话框时锁定orientationManager(true);
当前屏幕方向,并在对话框关闭时解锁orientationManager(false);
。