其中传感器旋转android手机? [英] Which sensor for rotating android phone?
问题描述
想象一下,你指着电视屏幕。你有你的手,你的手机心惊肉跳。现在,转动你的手腕。
Imagine you are pointing at the TV. You have your phone gripped in your hand. Now, rotate your wrist.
我需要的传感器来管理,以检测这种运动?
Which sensor would I need to manage to detect such a movement?
陀螺仪? 方向? 加速度计?
Gyroscope? Orientation? Accelerometer?
推荐答案
传感器 TYPE_MAGNETIC_FIELD
和 TYPE_ACCELEROMETER
的罚款来检测(如 TYPE_ORIENTATION
现在是很precated)。
The sensors TYPE_MAGNETIC_FIELD
and TYPE_ACCELEROMETER
are fine to detect that (as TYPE_ORIENTATION
is now deprecated).
您需要:
几个矩阵:
private float[] mValuesMagnet = new float[3];
private float[] mValuesAccel = new float[3];
private float[] mValuesOrientation = new float[3];
private float[] mRotationMatrix = new float[9];
侦听器捕捉传感器发送的值(这将是 SensorManager.registerListener()
,你将不得不调用设置您的传感器参数):
a listener to catch the values the sensors send (this will be an argument of SensorManager.registerListener()
that you will have to call to setup your sensors):
private final SensorEventListener mEventListener = new SensorEventListener() {
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
// Handle the events for which we registered
switch (event.sensor.getType()) {
case Sensor.TYPE_ACCELEROMETER:
System.arraycopy(event.values, 0, mValuesAccel, 0, 3);
break;
case Sensor.TYPE_MAGNETIC_FIELD:
System.arraycopy(event.values, 0, mValuesMagnet, 0, 3);
break;
}
};
和你需要计算方位角,俯仰和横滚:
And you'll need to compute the azimuth, pitch, and roll:
SensorManager.getRotationMatrix(mRotationMatrix, null, mValuesAccel, mValuesMagnet);
SensorManager.getOrientation(mRotationMatrix, mValuesOrientation);
mValuesOrientation
然后填充:
-
mValuesOrientation [0]
:方位角,绕Z轴旋转。 -
mValuesOrientation [1]
:间距,绕X轴旋转。 -
mValuesOrientation [2]
:辊,绕Y轴旋转。
mValuesOrientation[0]
: azimuth, rotation around the Z axis.mValuesOrientation[1]
: pitch, rotation around the X axis.mValuesOrientation[2]
: roll, rotation around the Y axis.
检查<一href="http://developer.android.com/reference/android/hardware/SensorManager.html#getOrientation%28float%5B%5D,%20float%5B%5D%29">documentation getOrientation的()知道如何轴的定义。您可能需要使用 SensorManager.remapCoordinateSystem()
来重新定义这些轴。
Check the documentation of getOrientation() to know how the axis are defined. You may need to use SensorManager.remapCoordinateSystem()
to redefine these axis.
这篇关于其中传感器旋转android手机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!