Android 屏幕方向传感器

发布于 2024-10-12 16:49:30 字数 315 浏览 5 评论 0原文

我想通过设置“它工作正常”来强制屏幕方向在单击按钮时横向

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

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

I want to force the screen orientation to landscape on button click by setting

 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

It works fine. Now I want the application to follow the sensor so that orientation is brought back to portrait when tilted back to portrait. I know this is possible by setting setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); but don't know where to set it. If the orientation is forced to landscape, orientation will remain in landscape no matter you tilt in any direction. Can anyone specify how to reset the orientation flag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夜血缘 2024-10-19 16:49:30

当前的 YouTube 应用可以满足您的要求。
我在视频播放应用程序中处理过同样的问题。

如果用户在纵向时强制方向为横向,请初始化 OrientationEventListener,它会通知您来自 SensorManager 的设备方向(范围从 0 到 360)

观察设备是否倾斜到大约 (方向 >= 60 && 方向 <= 120) || 的横向方向范围(方向 >= 240 && 方向 <= 300) 并将此状态保存到枚举或标志,然后如果设备返回到纵向方向范围 (方向 <= 40 | |orientation >= 320),检查枚举/标志并调用 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); ,重置标志/枚举并禁用传感器,直到用户再次强制定向。< br>
这是演示代码:

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

Current YouTube app does what you are asking for.
I've dealt with same kind of problem in my application for video playback.

If user forces orientation to landscape when he/she was in portrait, initialise OrientationEventListener which notifies you on device orientation from SensorManager which ranges from 0 to 360.

Watch if device tilts to landscape orientation range which would be around (orientation >= 60 && orientation <= 120) || (orientation >= 240 && orientation <= 300) and save this state to a enum or a flag and then if device goes back to Portrait orientation range (orientation <= 40 || orientation >= 320), check the enum/flag and call setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); , reset the flag/enum and disable the sensor until user force orientation again.

Here is the demo code:

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();
}

This worked similar to YouTube functionality, hope this helps.

一刻暧昧 2024-10-19 16:49:30

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

I think here you should use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)

衣神在巴黎 2024-10-19 16:49:30

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

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

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

It depends on when you want the sensor to detect rotation again.

Personally in an app I'm developping I have one specific activity where I need to be in portrait mode, so I use setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); in the onResume() of this activity and setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR); in onPause() and it works just fine, when I enter the activity it sets to portrait if it's not and doesn't allow to change and on exitting the sensor works again...

Where are you trying to enable and disable the sensor?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文