如何检测从横向到横向180度的屏幕旋转?
我见过其他一些类似的问题,但没有答案。如何检测从横向到横向180度的屏幕旋转?
从纵向旋转到横向(任一方向)并再次旋转,我们得到onConfigurationChanged()的有用调用。
但是,当从风景旋转到风景(通过180度)时,onConfigurationChanged()不会被调用。
我已经看到使用OrientationEventListener的提及,但这对我来说似乎是一片空白,因为您可以快速旋转而不触发显示方向更改。
我试着添加一个布局更改侦听器,但没有成功。
所以问题是,如何可靠地检测到这种横向取向的变化?
可能是你应该在你的OrientationEventListener添加一些逻辑代码:
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();
}
这有助于我感谢.. – 2014-04-03 11:14:04
为什么只有90和270度?手机通常不能颠倒,但平板电脑可以。同样的问题是当你旋转180度。从肖像。 – Oliv 2016-03-10 13:37:10
当心这个答案! OrientationEventListener不是用于180°方向更改的正确工具,因为它会实时触发,甚至在getRotation()更改前也会触发! 'DisplayListener'做的更好,因为它只在getRotation()'改变时才会触发。 – 0101100101 2018-02-21 23:52:55
我用这个代码把它用于我的情况下工作。
OrientationEventListener mOrientationEventListener = new OrientationEventListener(mActivity)
{
@Override
public void onOrientationChanged(int orientation)
{
if (orientation == ORIENTATION_UNKNOWN) return;
int rotation = mActivity.getWindowManager().getDefaultDisplay().getRotation();
switch (rotation) {
case Surface.ROTATION_0:
android.util.Log.i(TAG, "changed ROTATION_0 - " + orientation);
break;
case Surface.ROTATION_90:
android.util.Log.i(TAG, "changed ROTATION_90 - " + orientation);
break;
case Surface.ROTATION_180:
android.util.Log.i(TAG, "changed ROTATION_180 - " + orientation);
break;
case Surface.ROTATION_270:
android.util.Log.i(TAG, "changed ROTATION_270 - " + orientation);
break;
}
if ((rotation != mLastRotation) && (rotation & 0x1) == (mLastRotation & 0x1))
{
android.util.Log.i(TAG, "unhandled orientation changed >>> " + rotation);
}
mLastRotation = rotation;
}
};
if (mOrientationEventListener.canDetectOrientation()){
mOrientationEventListener.enable();
}
当心这个答案! OrientationEventListener不是用于180°方向更改的正确工具,因为它会实时触发,甚至在getRotation()更改前也会触发! 'DisplayListener'做的更好,因为它只在getRotation()'改变时才会触发。 – 0101100101 2018-02-21 23:53:02
当设备未旋转/移动时,OrientationEventlistener将不起作用。
我发现显示监听器是一种更好的方式来检测变化。
DisplayManager.DisplayListener mDisplayListener = new DisplayManager.DisplayListener() {
@Override
public void onDisplayAdded(int displayId) {
android.util.Log.i(TAG, "Display #" + displayId + " added.");
}
@Override
public void onDisplayChanged(int displayId) {
android.util.Log.i(TAG, "Display #" + displayId + " changed.");
}
@Override
public void onDisplayRemoved(int displayId) {
android.util.Log.i(TAG, "Display #" + displayId + " removed.");
}
};
DisplayManager displayManager = (DisplayManager) mContext.getSystemService(Context.DISPLAY_SERVICE);
displayManager.registerDisplayListener(mDisplayListener, UIThreadHandler);
我目前遇到同样的问题。你有没有找到解决方案? – 2013-01-23 03:04:56
对不起,目前为止没有。 – 2013-01-23 06:15:58
我有同样的问题。请解决任何问题?请发帖如果你发现它...我已经失去了我的这一周在这 – 2013-02-07 12:56:26