2011-01-19 134 views
9

我想通过设置Android的屏幕方向传感器

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

它正常工作,迫使屏幕方向上的按钮,点击景观。现在,我希望应用程序遵循传感器,以便在倾斜回肖像时将方向恢复为纵向。我知道这可以通过设置setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);但不知道在哪里设置。如果方向被强制为横向,无论您向任何方向倾斜,方向都将保持在横向。任何人都可以指定如何重置方向标志?

回答

1

这取决于您希望传感器何时再次检测到旋转。

个人在一个应用程序,我深化发展我有一个特定的活动,我需要在纵向模式,所以我在这个活动中onPause()onResume()setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);和它工作得很好,当我进入活动设置为纵向如果它不是并且不允许改变并且退出传感器再次工作...

你在哪里试图启用和禁用传感器?

+0

默认情况下,我的应用程序处于肖像模式。用户可以通过单击按钮或倾斜设备切换到横向模式。但在横向模式下,无法将活动恢复到纵向模式。如果必须提供另一个按钮来强制指向肖像,那么这将是最后一个选项。我想知道是否可以在逼近风景后设置为传感器模式。我在暂停时尝试将选项设置为传感器模式,但当设备处于纵向模式时,如果我想强制横向模式,它会恢复为纵向模式。 – Kulai 2011-01-19 07:43:39

4

我觉得这里应该使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)

7

当前YouTube应用做你所要求的。
我在处理视频播放应用程序时遇到了同样的问题。

如果用户强制方向为横向时,他/她在肖像,初始化OrientationEventListener它通知你设备的方向从SensorManager取值范围从0到360

看,如果设备倾斜至横向范围这将是围绕(orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300)并将此状态保存为枚举或标志,然后如果设备返回到纵向方向范围(orientation <= 40 || orientation >= 320),请检查枚举/标志并调用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);,重置标志/枚举并禁用传感器,直到用户再次强制方向为止。

下面是演示代码:

private enum SensorStateChangeActions { 
     WATCH_FOR_LANDSCAPE_CHANGES, SWITCH_FROM_LANDSCAPE_TO_STANDARD, WATCH_FOR_POTRAIT_CHANGES, SWITCH_FROM_POTRAIT_TO_STANDARD; 
} 

private SensorStateChangeActions mSensorStateChanges; 

public void goFullScreen() { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE); 
     mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES; 
     if (null == sensorEvent) 
      initialiseSensor(true); 
     else 
      sensorEvent.enable(); 
} 

public void shrinkToPotraitMode() { 
     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     mSensorStateChanges = SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES; 
     if (null == sensorEvent) 
      initialiseSensor(true); 
     else 
      sensorEvent.enable(); 
} 
/** 
* Initialises system sensor to detect device orientation for player changes. 
* Don't enable sensor until playback starts on player 
* 
* @param enable if set, sensor will be enabled. 
*/ 
private void initialiseSensor(boolean enable) { 
    sensorEvent = new OrientationEventListener(this, 
      SensorManager.SENSOR_DELAY_NORMAL) { 

     @Override 
     public void onOrientationChanged(int orientation) { 
      /* 
      * This logic is useful when user explicitly changes orientation using player controls, in which case orientation changes gives no callbacks. 
      * we use sensor angle to anticipate orientation and make changes accordingly. 
      */ 
      if (null != mSensorStateChanges 
        && mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_LANDSCAPE_CHANGES 
        && ((orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300))) { 
       mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD; 
      } else if (null != mSensorStateChanges 
        && mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_LANDSCAPE_TO_STANDARD 
        && (orientation <= 40 || orientation >= 320)) { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
       mSensorStateChanges = null; 
       sensorEvent.disable(); 
      } else if (null != mSensorStateChanges 
        && mSensorStateChanges == SensorStateChangeActions.WATCH_FOR_POTRAIT_CHANGES 
        && ((orientation >= 300 && orientation <= 359) || (orientation >= 0 && orientation <= 45))) { 
       mSensorStateChanges = SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD; 
      } else if (null != mSensorStateChanges 
        && mSensorStateChanges == SensorStateChangeActions.SWITCH_FROM_POTRAIT_TO_STANDARD 
        && ((orientation <= 300 && orientation >= 240) || (orientation <= 130 && orientation >= 60))) { 
       setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); 
       mSensorStateChanges = null; 
       sensorEvent.disable(); 
      } 
     } 
    }; 
    if (enable) 
     sensorEvent.enable(); 
} 

这个工作与YouTube类似的功能,希望这有助于。

+0

这正是我所期待的:一种模仿YouTube应用程序的方式!谢谢:) – wblaschko 2015-03-26 23:39:11