我需要获得更新的设备方向,但我必须将我的活动修复为Portrait(我在布局xml文件中做了),这阻止我使用它:
int rotation = getWindowManager().getDefaultDisplay().getRotation();
因为它总是给我画像旋转,
所以,我试图依靠传感器。 我发现不推荐使用Sensor.TYPE_ORIENTATION ,因此我使用了Sensor.TYPE_ACCELEROMETER和Sensor.TYPE_MAGNETIC_FIELD的组合,这里是事件监听器:
SensorEventListener sensorEventListener = new SensorEventListener() { float[] mGravity; float[] mGeomagnetic; @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) mGravity = event.values; if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) mGeomagnetic = event.values; if (mGravity != null && mGeomagnetic != null) { float R[] = new float[9]; float I[] = new float[9]; boolean success = SensorManager.getRotationMatrix(R, I, mGravity, mGeomagnetic); if (success) { float orientationData[] = new float[3]; SensorManager.getOrientation(R, orientationData); azimuth = orientationData[0]; pitch = orientationData[1]; roll = orientationData[2]; // now how to use previous 3 values to calculate orientation } } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-generated method stub } };
现在问题是 ,如何使用3个值的azimuth , pitch和roll来检测当前的设备方向,如下所示:
风景(旋转0)
人像(旋转90)
反向横向(旋转180)
反转人像(旋转270)
我发现它&这里是在读取pitch和roll后将在侦听器内调用的计算函数:
public static final int ORIENTATION_PORTRAIT = 0; public static final int ORIENTATION_LANDSCAPE_REVERSE = 1; public static final int ORIENTATION_LANDSCAPE = 2; public static final int ORIENTATION_PORTRAIT_REVERSE = 3; public int orientation = ORIENTATION_PORTRAIT; private int calculateOrientation(int roll, int pitch) { if (((orientation == ORIENTATION_PORTRAIT || orientation == ORIENTATION_PORTRAIT_REVERSE) && (roll > -30 && roll < 30))) { if (averagePitch > 0) return ORIENTATION_PORTRAIT_REVERSE; else return ORIENTATION_PORTRAIT; } else { // divides between all orientations if (Math.abs(pitch) >= 30) { if (pitch > 0) return ORIENTATION_PORTRAIT_REVERSE; else return ORIENTATION_PORTRAIT; } else { if (averageRoll > 0) { return ORIENTATION_LANDSCAPE_REVERSE; } else { return ORIENTATION_LANDSCAPE; } } } }
– 更新 –
这是我的完整实用程序类实现
– 更新 –
添加此图像以帮助可视化azimuth , pitch和roll :