【问题标题】:Android screen orientation to sensorAndroid 屏幕方向到传感器
【发布时间】:2011-01-19 06:43:54
【问题描述】:

我想通过设置在按钮点击时强制屏幕方向为横向

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

它工作正常。现在我希望应用程序跟随传感器,以便在倾斜回纵向时将方向恢复为纵向。我知道通过设置setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); 可以做到这一点,但不知道在哪里设置。如果方向被强制为横向,无论您向任何方向倾斜,方向都将保持为横向。谁能指定如何重置方向标志?

【问题讨论】:

    标签: android orientation


    【解决方案1】:

    当前的 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 功能类似,希望对您有所帮助。

    【讨论】:

    • 这正是我一直在寻找的:一种模仿 YouTube 应用正在做的事情的方法!谢谢:)
    • @CodeFury 如何使用这个以及什么是sensorEvent?
    • 这个答案真的很好
    • 您能展示如何在项目或演示项目中使用此代码吗?
    【解决方案2】:

    我认为你应该在这里使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)

    【讨论】:

      【解决方案3】:

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

      在我正在开发的应用程序中,我有一个特定的活动需要处于纵向模式,因此我在此活动的onResume() 中使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);,在onPause() 中使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);,它可以工作很好,当我进入活动时,它设置为纵向,如果不是并且不允许更改并且退出传感器再次工作......

      您尝试在哪里启用和禁用传感器?

      【讨论】:

      • 我的应用程序默认处于纵向模式。用户可以通过单击按钮或倾斜设备来切换到横向模式。但在横向模式下,无法将活动恢复为纵向模式。如果必须提供另一个按钮来强制方向为纵向,那么这将是最后一个选项。我想知道是否可以在强制横向后设置为传感器模式。我尝试了暂停时将您的选项设置为传感器模式,但是当我想在设备处于纵向模式时强制进入横向模式时,它将恢复为纵向模式。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-09
      • 2011-07-17
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      相关资源
      最近更新 更多