2014-10-20 22 views
1

我的應用程序使用相機。要以正確的方式顯示相機的預覽,我必須考慮相對於物理設備方向的活動方向。 I. e。如果活動方向被鎖定並且永不改變,我不需要採取任何進一步的步驟 - 隨着設備旋轉,預覽圖像將相應地旋轉。但是,想象我的活動可以改變方向。您旋轉設備並進行預覽 - 直到您到達肖像模式(假設它最初是風景),此時活動會旋轉以適應新的方向。但是預覽圖像隨之旋轉,現在它與相機和周圍的現實不同步。我必須做的是確定活動方向並相應地旋轉圖像。如何獲得活動相對於設備的正文的真實旋轉

看來Display.getRotation()可以用於此。但顯然,它不能:https://groups.google.com/forum/#!topic/android-developers/ij_0QbApKKc

問題是,原點不是由Android API修復的。一些平板電腦在正常方向(風景,音量按鈕向上)返回0的旋轉,以及其他一些(如我的Nexus 7 2013)返回1.

如何解決此問題?

+0

還不如更新您的問題標題和內容,目前還不清楚你想要什麼。旋轉或方向。 – Simas 2014-10-23 07:30:50

+0

很難弄清楚你在問什麼。但它聽起來像你只需要知道你的活動是在縱向還是橫向模式下運行。這很容易通過比較屏幕的寬度/高度來完成。如果高度>寬度,則它處於縱向模式,否則處於橫向模式。 – 2014-10-29 20:19:35

+0

@GregMiller:對不起,我沒有意識到我的英語不好。不知道如何陳述這個問題。不,你弄錯了,那不是我的意思。這不是我的Android開發第一年,我當然不需要爲一件簡單的事情提出一個新的問題,更不用說懸賞。簡而言之:我需要的是'Display.getRotation()'的功能。沒有更多,不少。問題是'Display.getRotation()'不起作用,並且在問題中描述了它不起作用的確切方式。 – 2014-10-29 21:47:10

回答

1

可以使用OrientationEventListener事情就這樣

mOrientationEventListener = new OrientationEventListener(context, SensorManager.SENSOR_DELAY_NORMAL) { 

      @Override 
      public void onOrientationChanged(int orientation) { 

       // determine our orientation based on sensor response 

       Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();           

       if (display.getOrientation() == Surface.ROTATION_0) { // landscape oriented devices 
        isLandscapeOriented = true; 
        if (orientation >= 315 || orientation < 45) { 
         if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) {       
          mOrientation = ORIENTATION_LANDSCAPE_NORMAL; 
         } 
        } else if (orientation < 315 && orientation >= 225) { 
         if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) { 
          mOrientation = ORIENTATION_PORTRAIT_INVERTED; 
         }      
        } else if (orientation < 225 && orientation >= 135) { 
         if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) { 
          mOrientation = ORIENTATION_LANDSCAPE_INVERTED; 
         }      
        } else if (orientation <135 && orientation > 45) { 
         if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) { 
          mOrientation = ORIENTATION_PORTRAIT_NORMAL; 
         }      
        }      
       } else { // portrait oriented devices 

        if (orientation >= 315 || orientation < 45) { 
         if (mOrientation != ORIENTATION_PORTRAIT_NORMAL) {       
          mOrientation = ORIENTATION_PORTRAIT_NORMAL; 
         } 
        } else if (orientation < 315 && orientation >= 225) { 
         if (mOrientation != ORIENTATION_LANDSCAPE_NORMAL) { 
          mOrientation = ORIENTATION_LANDSCAPE_NORMAL; 
         }      
        } else if (orientation < 225 && orientation >= 135) { 
         if (mOrientation != ORIENTATION_PORTRAIT_INVERTED) { 
          mOrientation = ORIENTATION_PORTRAIT_INVERTED; 
         }      
        } else if (orientation <135 && orientation > 45) { 
         if (mOrientation != ORIENTATION_LANDSCAPE_INVERTED) { 
          mOrientation = ORIENTATION_LANDSCAPE_INVERTED; 
         }      
        } 
       } 


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

唐`確定的定向忘記調用mOrientationEventListener.disable();

編輯:

public static void setCameraDisplayOrientation(Activity activity, 
     int cameraId, android.hardware.Camera camera) { 

    android.hardware.Camera.CameraInfo info = 
      new android.hardware.Camera.CameraInfo(); 
    android.hardware.Camera.getCameraInfo(cameraId, info); 

    int result; 
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     result = (info.orientation + degrees) % 360; 
     result = (360 - result) % 360; // compensate the mirror 
    } else { // back-facing 
     int rotation = activity.getWindowManager().getDefaultDisplay() 
      .getRotation(); 
     int degrees = 0; 
     switch (rotation) { 
     case Surface.ROTATION_0: degrees = 0; break; 
     case Surface.ROTATION_90: degrees = 90; break; 
     case Surface.ROTATION_180: degrees = 180; break; 
     case Surface.ROTATION_270: degrees = 270; break; 
     } 
     result = (info.orientation - degrees + 360) % 360; 
    } 
    camera.setDisplayOrientation(result); 
} 
+0

我應該什麼時候調用'disable',如果沒有,會發生什麼? – 2014-10-24 13:30:54

+0

此外,您的代碼不完整。我在哪裏設置這個監聽器?哪個傳感器,如何初始化? – 2014-10-24 13:34:13

+0

其實你不需要設置監聽器--mOrientationEventListener.enable()應該足夠了。你可以把它放在你的活動中。我在onCreate中啓用並禁用onDestroy。 而不是調用禁用 - 是不好的做法,因爲mOrientationEventListener的實例將佔用您的內存資源,直到應用程序將被關閉。 – 2014-10-24 15:40:27