开发者

Android screen orientation to sensor

开发者 https://www.devze.com 2023-02-05 05:02 出处:网络
I want to force the screen orientation to landscape on button click by setting setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

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 lands开发者_StackOverflow中文版cape, orientation will remain in landscape no matter you tilt in any direction. Can anyone specify how to reset the orientation flag?


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.


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消