使用 Android 设备检测运动
我正在开发一个 Android 2.2 应用程序。
我想知道用户何时向上或向下移动设备,何时向左或向右移动。垂直安装时,设备将处于静止状态。换句话说,将相机(Y 轴沿相机轴)用于需要旋转角度的增强现实应用程序:
remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);
这是我的代码:
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
accelValues = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
magneValues = event.values;
updateOrientation(calculateOrientation());
}
private float[] calculateOrientation() {
float[] values = new float[3];
float[] R = new float[9];
float[] outR = new float[9];
SensorManager.getRotationMatrix(R, null, accelValues, magneValues);
SensorManager.remapCoordinateSystem(R,
SensorManager.AXIS_X,
SensorManager.AXIS_Z,
outR);
SensorManager.getOrientation(outR, values);
// Convert from Radians to Degrees.
values[0] = (float) Math.toDegrees(values[0]); // Azimuth, rotation around Z
values[1] = (float) Math.toDegrees(values[1]); // Pitch, rotation around X
values[2] = (float) Math.toDegrees(values[2]); // Roll, rotation around Y
return values;
}
但我不确定如何知道用户是否将设备向左移动或向右。
而且,我如何知道用户是否行走?
有一个方位角、一个俯仰角和一个横滚角,但我不知道如何使用这些值。
有什么建议吗?
I'm developing an Android 2.2 application.
I want to know when user moves device up or down, and when it moves to the left or to the right. The device will be at rest when mounted vertically. In other words, using the camera (Y axis along the camera's axis) for an augmented reality application where the rotation angles are needed:
remapCoordinateSystem(inR, AXIS_X, AXIS_Z, outR);
This is my code:
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
accelValues = event.values;
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
magneValues = event.values;
updateOrientation(calculateOrientation());
}
private float[] calculateOrientation() {
float[] values = new float[3];
float[] R = new float[9];
float[] outR = new float[9];
SensorManager.getRotationMatrix(R, null, accelValues, magneValues);
SensorManager.remapCoordinateSystem(R,
SensorManager.AXIS_X,
SensorManager.AXIS_Z,
outR);
SensorManager.getOrientation(outR, values);
// Convert from Radians to Degrees.
values[0] = (float) Math.toDegrees(values[0]); // Azimuth, rotation around Z
values[1] = (float) Math.toDegrees(values[1]); // Pitch, rotation around X
values[2] = (float) Math.toDegrees(values[2]); // Roll, rotation around Y
return values;
}
but I'm not sure how can I know if user moves device to the left or to the right.
And, how can I know if user walk?
There is an Azimuth, a Pitch and a Roll, but I don't know how can I use these values.
Any advice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论