自动旋转关闭时获取设备旋转
我正在通过设置android:screenOrientation="nosensor"
来设置相机应用程序,该应用程序在相机预览期间禁用自动旋转功能。不过,我仍然想知道拍摄照片时手机的旋转。 getResources().getConfiguration().orientation
不够具体,只返回纵向,横向或正方形。 getWindowManager().getDefaultDisplay().getRotation()
始终为0,因为屏幕被强制为默认方向 - 如果自动旋转开启,我怎么能得到将的值?自动旋转关闭时获取设备旋转
在活动中,你可以使用
@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
如果您对“将会”好奇,您将不得不使用陀螺仪并实时检测设备的实际旋转。 – 2013-04-08 05:09:24
你可以在这里得到帮助 http://stackoverflow.com/questions/4697631/android-screen-orientation – manish 2013-04-08 05:16:51