我正在通過設置android:screenOrientation="nosensor"
來設置相機應用程序,該應用程序在相機預覽期間禁用自動旋轉功能。不過,我仍然想知道拍攝照片時手機的旋轉。 getResources().getConfiguration().orientation
不夠具體,只返回縱向,橫向或正方形。 getWindowManager().getDefaultDisplay().getRotation()
始終爲0,因爲屏幕被強制爲默認方向 - 如果自動旋轉開啓,我怎麼能得到將的值?自動旋轉關閉時獲取設備旋轉
回答
使用this類用於在設備的方向已更改時從SensorManager接收通知。
在活動中,你可以使用
@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
}
這個方法調用你改變設備配置後。
請注意,通過配置報告的方向更改機制僅報告橫向或縱向,而不是包括縱向向上的完整方向集向下和風景左轉與風景右轉。 – bleater 2014-04-07 02:14:43
你可以聽的配置改變
<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會被調用。
這是有點深入但很好地跟蹤方向的變化。當相機意圖打開並拍攝照片時,我使用它來獲取照片方向。然後我知道照片是什麼方向。它顯然不適用於照片庫,但您可以使用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);
}
完美的解決方案 – Praveen 2014-12-27 14:59:59
- 1. 用旋轉設備關閉旋轉時的UIPopover
- 2. 設備旋轉時提供自己的旋轉動畫
- 3. 暫時關閉自動屏幕旋轉
- 4. IOS旋轉設備不旋轉動畫
- 5. 設備旋轉時旋轉相機
- 6. 設備旋轉前關閉UIActionSheet?
- 7. 360旋轉關閉設置
- 8. 旋轉活動關閉
- 9. 設備旋轉/旋轉計數
- 10. 不旋轉設備的頁面旋轉
- 11. android設備旋轉
- 12. 當設備旋轉
- 13. 設備上的應用程序旋轉但旋轉選項已關閉
- 14. 如何在設備旋轉時關閉ABNewPersonViewController的keyboad?
- 15. 設備旋轉時應用程序關閉
- 16. 當設備旋轉時,使子視圖也旋轉iPhone/iPad
- 17. UIImagePickerController在設備旋轉時旋轉iOS 7
- 18. 使Android旋轉木馬自動旋轉
- 19. 默認設備旋轉關閉時,是否可以更改設備的方向?
- 20. 從旋轉矩陣獲取有關y軸的旋轉
- 21. 處理設備旋轉
- 22. iOS6設備旋轉限制
- 23. TSAlertView啓用設備旋轉
- 24. 安卓設備旋轉
- 25. CoreAnimation和設備旋轉
- 26. 當設備被鎖定,旋轉並重新鎖定時獲得強制關閉
- 27. 相機app.Rotate圖片自動旋轉關閉時的按鈕
- 28. 關閉時自動旋轉屏幕不被尊重
- 29. Dialog在屏幕旋轉時關閉android
- 30. MPMoviePlayerController自動旋轉?
如果您對「將會」好奇,您將不得不使用陀螺儀並實時檢測設備的實際旋轉。 – 2013-04-08 05:09:24
你可以在這裏得到幫助 http://stackoverflow.com/questions/4697631/android-screen-orientation – manish 2013-04-08 05:16:51