Android-'sensorPortrait'方向不起作用 [英] Android - 'sensorPortrait' orientation not working

查看:259
本文介绍了Android-'sensorPortrait'方向不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个方向 sensorPortrait 不起作用的问题,我尝试通过清单和活动本身启用

I have come across a problem where the orientation sensorPortrait does not work, i have tried enabling both through the manifest and within the activity itself with

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);

但这似乎只是在正常人像模式下锁定,但是如果我尝试使用"fullSensor"

But this seems to just be locked in normal portrait mode, however if i try `fullSensor'

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);

根据文档

方向由设备方向传感器针对4个方向中的任何一个确定.这类似于传感器",除了它允许4种可能的屏幕方向中的任何一种,而与设备的正常运行方式无关(例如,某些设备通常不会使用反向肖像或反向风景,但是启用了这些功能).在API级别9中添加.

The orientation is determined by the device orientation sensor for any of the 4 orientations. This is similar to "sensor" except this allows any of the 4 possible screen orientations, regardless of what the device will normally do (for example, some devices won't normally use reverse portrait or reverse landscape, but this enables those). Added in API level 9.

它确实做到了,所有4个方向都是可能的.如果我也尝试

and it does exactly that, all 4 orientations are possible. If i also try

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

我能够获得反向​​肖像,这使我回到原来的问题,为什么 sensorPortrait 不起作用?看起来与"fullSensor"文档中的这一行有关

I am able to achieve reverse portrait, which leads me back to my original question, why does sensorPortrait not work? It looks like it has something to do with this line from the docs for `fullSensor'

不管设备通常会做什么(例如,某些设备通常不会使用反向人像或反向风景,但启用了这些功能)

regardless of what the device will normally do (for example, some devices won't normally use reverse portrait or reverse landscape, but this enables those)

那我怎么启用它呢?为什么 fullSensor 似乎会覆盖它而不是 sensorPortrait ?我似乎找不到任何提及方法.这个问题表明 PhoneWindowManager 对此负责.

So how do i enable it, is that possible and why does fullSensor seem to override it but not sensorPortrait? I can't seem to find any mention of how to do so. This question suggests that the PhoneWindowManager is responsible for this.

理想的解决方案是仅创建一个 OrientationEventListener()并手动调用 setRequestedOrientation(),具体取决于通过 onOrientationChanged(int orientation)<返回的值/code>?

Is the ideal solution just to create an OrientationEventListener() and manually call setRequestedOrientation() manually depending on the values returned via onOrientationChanged(int orientation)?

推荐答案

作为解决方案,我创建了 SensorPortraitActivity :

As a work around i have created a SensorPortraitActivity:

public class SensorPortraitActivity extends AppCompatActivity {

    private static final int PORTRAIT = 0;
    private static final int REVERSE_PORTRAIT = 180;
    private static final int OFFSET = 45;
    private static final int UNKNOWN = -1;

//  account for 0 = 360 (eg. -1 = 359)
    private static final int PORTRAIT_START = PORTRAIT - OFFSET + 360; 
    private static final int PORTRAIT_END = PORTRAIT + OFFSET;
    private static final int REVERSE_PORTRAIT_START = REVERSE_PORTRAIT - OFFSET;
    private static final int REVERSE_PORTRAIT_END = REVERSE_PORTRAIT + OFFSET;

    private OrientationChangeListener mListener;
    private OrientationEventListener mOrientationListener;
    private CurrentOrientation mCurrentOrientation;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mOrientationListener = new OrientationEventListener(this) {
            @Override
            public void onOrientationChanged(int i) {
                orientationChanged(i);
            }
        };
    }

    @Override
    protected void onResume() {
        super.onResume();
        mOrientationListener.enable();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mOrientationListener.disable();
    }

    //optional 
    public void setOrientationChangeListener(OrientationChangeListener listener){
        mListener = listener;
    }

    private void orientationChanged(int degrees) {

        if (degrees != UNKNOWN){

            if (degrees >= PORTRAIT_START || degrees <= PORTRAIT_END){

                if (mCurrentOrientation != CurrentOrientation.PORTRAIT){

                    mCurrentOrientation = CurrentOrientation.PORTRAIT;
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

                if (mListener != null){
                    mListener.onPortrait();
                }
            }
        } else if (degrees >= REVERSE_PORTRAIT_START && degrees <= REVERSE_PORTRAIT_END){

            if (mCurrentOrientation != CurrentOrientation.REVERSE_PORTRAIT){

                mCurrentOrientation = CurrentOrientation.REVERSE_PORTRAIT;
                    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);

                    if (mListener != null) {
                        mListener.onReversePortrait();
                    }
                }
            }
        }
    }

    interface OrientationChangeListener {
        void onPortrait();
        void onReversePortrait();
    }

    enum CurrentOrientation {
        PORTRAIT, REVERSE_PORTRAIT
    }
}

尽管对于像这样简单的事情来说似乎有些过分了.

Although it does seem like overkill for something as simple as this.

要使用它,请简单地扩展SensorPortraitActivity

public class ExampleActivity extends SensorPortraitActivity implements SensorPortraitView.OrientationChangeListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //set listener if you want callbacks
        super.setOrientationChangeListener(this);
    }

    @Override
    public void onPortrait() {
        //portrait orientation
    }

    @Override
    public void onReversePortrait() {
        //reverse portrait orientation
    }
}

这篇关于Android-'sensorPortrait'方向不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆