2012-03-28 75 views
17

我見過其他一些類似的問題,但沒有答案。如何檢測從橫向到橫向180度的屏幕旋轉?

從縱向旋轉到橫向(任一方向)並再次旋轉,我們得到onConfigurationChanged()的有用調用。

但是,當從風景旋轉到風景(通過180度)時,onConfigurationChanged()不會被調用。

我已經看到使用OrientationEventListener的提及,但這對我來說似乎是一片空白,因爲您可以快速旋轉而不觸發顯示方向更改。

我試着添加一個佈局更改偵聽器,但沒有成功。

所以問題是,如何可靠地檢測到這種橫向取向的變化?

+0

我目前遇到同樣的問題。你有沒有找到解決方案? – 2013-01-23 03:04:56

+0

對不起,目前爲止沒有。 – 2013-01-23 06:15:58

+0

我有同樣的問題。請解決任何問題?請發帖如果你發現它...我已經失去了我的這一週在這 – 2013-02-07 12:56:26

回答

7

可能是你應該在你的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(); 
} 
+0

這有助於我感謝.. – 2014-04-03 11:14:04

+0

爲什麼只有90和270度?手機通常不能顛倒,但平板電腦可以。同樣的問題是當你旋轉180度。從肖像。 – Oliv 2016-03-10 13:37:10

+0

當心這個答案! OrientationEventListener不是用於180°方向更改的正確工具,因爲它會實時觸發,甚至在getRotation()更改前也會觸發! 'DisplayListener'做的更好,因爲它只在getRotation()'改變時纔會觸發。 – 0101100101 2018-02-21 23:52:55

3

我用這個代碼把它用於我的情況下工作。

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(); 
    } 
+0

當心這個答案! OrientationEventListener不是用於180°方向更改的正確工具,因爲它會實時觸發,甚至在getRotation()更改前也會觸發! 'DisplayListener'做的更好,因爲它只在getRotation()'改變時纔會觸發。 – 0101100101 2018-02-21 23:53:02

4

當設備未旋轉/移動時,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); 
+0

注意:這需要API Level 17+ – lapis 2014-05-14 19:08:32

+0

爲什麼當設備未旋轉/移動時,您需要使用OrientationEventlistener工作? – 2014-05-14 19:32:35

+0

我的應用程序需要提供新的方向狀態,但有時可以通過調用API來更改方向。 最後我用一個線程來拉它的狀態..沒有更好的解決方案,因爲這隻適用於API 17+。 – superuser 2014-05-22 14:57:15

相關問題