问题描述
我需要获取更新的设备方向,但是我必须将我的活动固定为Portrait(这是我在布局xml文件中所做的),这使我无法使用此功能:
I need to get the updated device orientation, but I have to fix my activity to Portrait (which I did in the layout xml file), which prevents me from using this:
int rotation = getWindowManager().getDefaultDisplay().getRotation();
因为它总是使我旋转人像,
because it always gives me the portrait rotation,
所以,我试图依靠传感器.我发现不推荐使用Sensor.TYPE_ORIENTATION
,因此我使用的是Sensor.TYPE_ACCELEROMETER
& Sensor.TYPE_MAGNETIC_FIELD
,这是事件侦听器:
So, I'm trying to rely on the sensors. I found that Sensor.TYPE_ORIENTATION
is deprecated, so I'm using a combination of Sensor.TYPE_ACCELEROMETER
& Sensor.TYPE_MAGNETIC_FIELD
and here is the event listener:
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
将当前设备方向检测为以下之一:
Now Question is, how to use the 3 values azimuth
, pitch
& roll
to detect current device orientation as one of these:
- 风景(旋转0)
- 肖像(旋转90度)
- 反向风景(旋转180度)
- 反像(旋转270度)
推荐答案
我已找到&这是在读取pitch
& roll
:
I've found it & here is the calculating function that will be called inside the listener after reading the 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;
}
}
}
}
-更新-
&这是我完整的实用程序类的实现
& here is my full utility class implementation
-更新-
添加此图像有助于可视化azimuth
,pitch
& roll
:
Adding this image to help visualizing the azimuth
, pitch
& roll
:
这篇关于Android:从方位角,滚动和滚动获取设备方向沥青的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!