如何检测手机的一整圈(360度)? [英] How to detect one full turn (360 degrees) of the phone?
问题描述
由于我没有在网络上找到任何相关的答案,因此我发布了一个有关使用加速度计检测android设备绕其轴 360度旋转的问题。例如,在横向模式下绕y轴:假设在起始位置y值为0(设备与地面平坦)。当电话向前旋转90度 y = -10
时,向前倾斜180度将导致 y = 0
, y = +10
倾斜270度,而 y = 0
倾斜360度。那么代码看起来如何仅在预定方向上检测到设备的这种完全转向?
Since I did not find any relevant answers searching the web, I am posting a question regarding detection of 360 degrees turns of android devices around its axes using the accelerometer. For example around y-axis in landscape mode: let us say that in starting position y value is 0 (device is flat to ground). When phone is rotated forward for 90 degrees y = -10
, 180 degrees forward tilt would result in y = 0
, 270 degrees tilt in y = +10
and 360 degrees in y = 0
. So how would the code look like to detect this full turn of the device only in prespecified direction?
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
// assign directions
x = event.values[1];
y = event.values[0];
z = event.values[2];
???
}
}
编辑: $ b
private float[] r = new float[9];
private float[] i = new float[9];
private float[] v = new float[3];
private float[] accValues = new float[3];
private float[] geoValues = new float[3];
private int azimuth, pitch, roll;
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
accValues = event.values.clone();
}
if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
geoValues = event.values.clone();
}
boolean success = SensorManager.getRotationMatrix(r, i, accValues,
geoValues);
if (success) {
SensorManager.getOrientation(r, v);
azimuth = v[0] * (180 / Math.PI);
pitch = v[1] * (180 / Math.PI);
roll = v[2] * (180 / Math.PI);
}
}
推荐答案
您可能会尝试:
`
@Override
`
@Override
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
x = event.values[1];
y = event.values[0];
z = event.values[2];
if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
x1 = event.values[1];
y1 = event.values[0];
z1 = event.values[2];
}
}`
此代码应允许您检测加速度和方向的变化,因此即使加速度计的Y值再次从0变为0,因为方向发生了变化,但旋转了一个整周(但是,您仍然需要清除此代码,以消除手机向某个方向旋转180°,然后又向后旋转180°(重新将手机上下移动)的情况)
This code should allow you to detect a change in acceleration and orientation, so even if the Y value of the accelerometer goes from 0 to 0 again because there was a change in orientation then there was a full rotation (however, u still need to clean this code to eliminate the case where the phone goes 180° in a direction and then goes back 180° (turn the phone up and down again ))
出于好奇,您为什么需要将手机旋转360°?我试图将手机旋转360°,这并非易事。
Out of curiosity, why would you need a 360° turn of the phone? I tried to turn my phone 360°, it's not something easy to do.
这篇关于如何检测手机的一整圈(360度)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!