如何通过180度景观横向检测屏幕旋转? [英] How to detect screen rotation through 180 degrees from landscape to landscape orientation?

查看:159
本文介绍了如何通过180度景观横向检测屏幕旋转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过类似这样的一些其他问题,但没有答案。

I've seen a few other questions similar to this but no answers.

从纵向旋转为横向(任意方向),然后再返回,我们得到了有益的调用onConfigurationChanged()。

Rotating from portrait to landscape (either direction) and back again, we get the helpful call to onConfigurationChanged().

不过,从横向旋转时的景观(180度)onConfigurationChanged()将不会调用。

However, when rotating from landscape to landscape (through 180 degrees) onConfigurationChanged() is not called.

我见过提及使用OrientationEventListener,但是这似乎古怪我,因为你可以快速旋转,周围没有触发的显示方向的改变。

I've seen mention of using OrientationEventListener but this seems flakey to me because you can rotate quickly around without triggering a display orientation change.

我已经尝试添加一个布局更改侦听器,但没有成功。

I've tried adding a layout change listener, but with no success.

所以现在的问题是,如何可靠地检测横向这样的变化?

So the question is, how to reliably detect such a change in landscape orientation?

推荐答案

可能是你应该增加一些逻辑code在OrientationEventListener是这样的:

May be you should add some logical code in your OrientationEventListener like this:

mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

OrientationEventListener orientationEventListener = new OrientationEventListener(this,
        SensorManager.SENSOR_DELAY_NORMAL) {
    @Override
    public void onOrientationChanged(int orientation) {

        Display display = mWindowManager.getDefaultDisplay();
        int rotation = display.getRotation();
        if ((rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && rotation != mLastRotation) {
            Log.i(TAG, "changed >>> " + rotation);

            // do something

            mLastRotation = rotation;
        }
    }
};

if (orientationEventListener.canDetectOrientation()) {
    orientationEventListener.enable();
}

这篇关于如何通过180度景观横向检测屏幕旋转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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