Abd*_*ady 7 android rotation android-sensors
我需要获得更新的设备方向,但我必须将我的活动修复为Portrait(我在布局xml文件中做了),这阻止我使用它:
int rotation = getWindowManager().getDefaultDisplay().getRotation();
Run Code Online (Sandbox Code Playgroud)
因为它总是给我画像旋转,
所以,我试图依靠传感器.我发现它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
}
};
Run Code Online (Sandbox Code Playgroud)
现在问题是,如何使用3个值azimuth,pitch以及roll检测当前设备方向为以下之一:
Abd*_*ady 13
我发现它&这里是读取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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
- 更新 -
这是我的完整实用程序类实现
- 更新 -
添加此图像以帮助可视化azimuth,pitch&roll:

| 归档时间: |
|
| 查看次数: |
13126 次 |
| 最近记录: |