gpt4 book ai didi

android - 在 v4.3 之前遵守 android 屏幕方向锁定

转载 作者:行者123 更新时间:2023-11-29 00:26:29 26 4
gpt4 key购买 nike

Android 4.3 引入了用于设置屏幕方向的 userLandscape、userPortrait 和 fullUser 值 - 以及相应的 SCREEN_ORIENTATION_XX 常量。这些基本上是说,如果屏幕方向被锁定,则相关显示器不会发生自动屏幕旋转,但解锁后会起作用。

简单的问题:在 4.3 之前,这是如何完成的?是否需要设置约束值并监视全局设置,以便在运行时更改屏幕方向以反射(reflect)这一点。或者有更好的方法吗?

最佳答案

我也很难找到这个问题的答案。 SENSOR_LANDSCAPE 似乎忽略了设备方向锁定,所以这就是我最终使用的。

// Is the device set to allow auto-rotation?
if (Settings.System.getInt(getContentResolver(),Settings.System.ACCELEROMETER_ROTATION, 0) == 1)
{
// Unlock rotation
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
else
{
// Lock rotation
setRequestedOrientation(getScreenOrientation());
}

获取当前屏幕方向有点复杂。我使用了此处提供的答案之一:

How do I get the CURRENT orientation (ActivityInfo.SCREEN_ORIENTATION_*) of an Android device?

private int getScreenOrientation()
{
WindowManager winMan = (WindowManager)getSystemService(Activity.WINDOW_SERVICE);
int rotation = winMan.getDefaultDisplay().getRotation();
DisplayMetrics dm = new DisplayMetrics();
winMan.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
int orientation;
// if the device's natural orientation is portrait:
if ((rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) && height > width ||
(rotation == Surface.ROTATION_90
|| rotation == Surface.ROTATION_270) && width > height)
{
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;
case Surface.ROTATION_270:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to portrait.");
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
}
}
// if the device's natural orientation is landscape or if the device
// is square:
else
{
switch(rotation)
{
case Surface.ROTATION_0:
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
case Surface.ROTATION_90:
orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
break;
case Surface.ROTATION_180:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
break;
case Surface.ROTATION_270:
orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
break;
default:
Log.e(TAG, "Unknown screen orientation. Defaulting to landscape.");
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
break;
}
}

return orientation;
}

关于android - 在 v4.3 之前遵守 android 屏幕方向锁定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18958333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com