Как обнаружить поворот экрана на 180 градусов от пейзажной к пейзажной ориентации?
Я видел несколько других вопросов, подобных этому, но ответов не было.
Вращаясь от портрета к пейзажу (в любом направлении) и обратно, мы получим полезный вызов onConfigurationChanged().
Однако при повороте с пейзажа на пейзаж (на 180 градусов) onConfigurationChanged() не вызывается.
Я видел упоминание об использовании OrientationEventListener, но для меня это кажется ошибочным, потому что вы можете быстро вращаться, не вызывая изменения ориентации отображения.
Я попробовал добавить прослушиватель изменений макета, но без успеха.
Итак, вопрос в том, как надежно обнаружить такое изменение ландшафтной ориентации?
Ответы
Ответ 1
Может быть, вы должны добавить логический код в свой 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();
}
Ответ 2
Я использую этот код, чтобы он работал для моего случая.
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();
}
Ответ 3
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);