Android游戏My Paper Plane是如何实现倾斜控制的一个很好的例子,但我一直在努力理解如何做类似的事情.
我有以下示例,它使用SensorManager中的getOrientation().整个事情都在这里的pastebin上.它只是将方向值打印到文本字段.这是最相关的片段:
private void computeOrientation() {
if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
m_lastMagFields, m_lastAccels)) {
SensorManager.getOrientation(m_rotationMatrix, m_orientation);
/* 1 radian = 57.2957795 degrees */
/* [0] : yaw, rotation around z axis
* [1] : pitch, rotation around x axis
* [2] : roll, rotation around y axis */
float yaw = m_orientation[0] * 57.2957795f;
float pitch = m_orientation[1] * 57.2957795f;
float roll = m_orientation[2] * 57.2957795f;
/* append returns an average of the last …Run Code Online (Sandbox Code Playgroud)