是否有可靠的方法在所有Android设备上锁定屏幕方向?以下代码适用于我的Nexus S和其他手机,但出于某种原因,ROTATION_90对应于Xoom上的SCREEN_ORIENTATION_REVERSE_PORTRAIT。
有没有办法可靠地将旋转映射到方向?
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;
}
编辑:此代码用于获取当前方向并将其锁定。方向暂时锁定,然后释放给用户。
答案 0 :(得分:34)
以下是我的解决方案,适用于任何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;
}
答案 1 :(得分:18)
我稍微修改了diyism的答案,以弥补你在版本2.3之前不能使用reverse_landscape和reverse_portrait模式的事实
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);
}
答案 2 :(得分:6)
临时屏幕锁定您可以轻松使用:
//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);
在显示对话框和执行重要的后台工作时将其用于临时屏幕锁定。
确保currentActivity在您尝试访问时有效,否则无效:)
祝你好运:)
答案 3 :(得分:3)
// 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);
}
}
答案 4 :(得分:2)
使用:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR)
因为方向取决于物理方向传感器:显示屏将根据用户移动设备的方式旋转。这允许4种可能的旋转中的任何一种,无论设备通常会做什么(例如某些设备通常不会使用180度旋转)。你的代码也应该在Xoom上工作......
答案 5 :(得分:2)
此解决方案仅基于其他解决方案。这是解决问题的另一种方法:在某些设备上格局为ROTATION_90
,而在(少数)其他设备上格林为ROTATION_270
。当我在Kindle Fire HD 7上尝试类似enl8enmentnow的解决方案时,它使屏幕上下颠倒,然后立即返回。我没有看到任何其他想法,而不是硬编码哪些设备认为风景为270,所以这是硬编码解决方案:
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()
检测设备是否为第二代Kindle或更高版本(请参阅this link,从this link获取MODEL
。我不知道是否还应该包括其他设备;如果您知道,请发表评论。
此外,在API&gt; = 18上,这只是使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED)
。我只在仿真器上测试过;如果它在真实设备上有问题,请发表评论。
答案 6 :(得分:1)
您可以在AndroidManifest.xml中将Activity声明为仅横向或纵向。只需将screenOrientation属性添加到activity元素:
http://developer.android.com/guide/topics/manifest/activity-element.html
答案 7 :(得分:-1)
我的解决方案:
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
}
}