2013-04-08 145 views
2

我正在通過設置android:screenOrientation="nosensor"來設置相機應用程序,該應用程序在相機預覽期間禁用自動旋轉功能。不過,我仍然想知道拍攝照片時手機的旋轉。 getResources().getConfiguration().orientation不夠具體,只返回縱向,橫向或正方形。 getWindowManager().getDefaultDisplay().getRotation()始終爲0,因爲屏幕被強制爲默認方向 - 如果自動旋轉開啓,我怎麼能得到的值?自動旋轉關閉時獲取設備旋轉

+0

如果您對「將會」好奇,您將不得不使用陀螺儀並實時檢測設備的實際旋轉。 – 2013-04-08 05:09:24

+0

你可以在這裏得到幫助 http://stackoverflow.com/questions/4697631/android-screen-orientation – manish 2013-04-08 05:16:51

回答

0

使用this類用於在設備的方向已更改時從SensorManager接收通知。

1

在活動中,你可以使用

@Override 
public void onConfigurationChanged(Configuration newConfig) { //this method return you latest configuration 
    // TODO Auto-generated method stub 
    super.onConfigurationChanged(newConfig); 
// newConfig.orientation //Using this you will able to get orientation of device 
} 

這個方法調用你改變設備配置後。

+0

請注意,通過配置報告的方向更改機制僅報告橫向或縱向,而不是包括縱向向上的完整方向集向下和風景左轉與風景右轉。 – bleater 2014-04-07 02:14:43

1

你可以聽的配置改變

<activity 
... 
      android:configChanges="orientation|screenSize" 
... 

在onConfigurationChanged(),您可以強制對方位的變化所需方向(如縱向),但得到的信息。

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    //get new configuration orientation from newConfig.orientation 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
} 

而且不要使用android:screenOrientation =「portrait」,所以當用戶改變方向時onConfigurationChanged會被調用。

4

這是有點深入但很好地跟蹤方向的變化。當相機意圖打開並拍攝照片時,我使用它來獲取照片方向。然後我知道照片是什麼方向。它顯然不適用於照片庫,但您可以使用exif信息。我驚奇地發現,三星Galaxy S3和S4並未從相機返回exif方向數據。

//keep a history 
private int [] _orientationHistory = new int[5]; 
private int _orientationIndex; 
private int _highestIndex = -1; 
private int _currentOrientation; 

//essentially compute the mode of the data. This could be improved 
protected int getOrientation() 
{ 
    if (_highestIndex < 0) return 0; 

    Arrays.sort(_orientationHistory); 
    return _orientationHistory[_highestIndex/2]; 
} 

OrientationEventListener _imageViewOrientationListener = new    
       OrientationEventListener(getContext(), SensorManager.SENSOR_DELAY_NORMAL) { 
public void onOrientationChanged(int angle) { 
     //angle comes in 360 degrees, convert that to a 0-3 rotation 
     //Get the angle in 90 degree increments, 0,90,180,270 
     //with 45 degrees tolerance in each direction (straight up is 0) 
     //this is the same as the data coming from getWindowManager().getDefaultDisplay().getRotation() 

     angle = angle + 45; 
     if (angle > 360) angle = angle - 360; 
     int orientation = angle/90; 

     //I use a history in order to smooth out noise 
     //and don't start sending events about the change until this history is filled 
     if (_orientationIndex > _highestIndex) { 
      _highestIndex = _orientationIndex; 
     } 
     _orientationHistory[_orientationIndex] = orientation; 
     _orientationIndex ++; 

     if (_orientationIndex == _orientationHistory.length) { 
      _orientationIndex = 0; 
     } 

     int lastOrientation = _currentOrientation; 
     //compute the orientation using above method 
     _currentOrientation = getOrientation(); 

     if (_highestIndex == _orientationHistory.length - 1 && lastOrientation != _currentOrientation) { 
      //enough data to say things changed 
      orientationChanged(lastOrientation, _currentOrientation); 
     } 
    } 
}; 

然後讓這個監聽器只需要添加這行代碼:

if (_imageViewOrientationListener != null) { 
    _imageViewOrientationListener.enable(); 
} 

最後,添加方法orientationChanged(),當方向改變接收更新。 如果你需要像我一樣,開始記錄這個信息,當你打開相機的活動,如果方向翻轉到不同的東西,比如從肖像風景,你可以假設在該時間段拍攝的圖片是旋轉改變你記錄。

protected void orientationChanged(int lastOrientation, int currentOrientation) { 
    Log.d(getClass().getSimpleName(), "Orientation changed to " + currentOrientation + " from " + lastOrientation); 
} 
+0

完美的解決方案 – Praveen 2014-12-27 14:59:59