Android 上的设备倾斜度
Device tilting degrees on Android
我正在开发一款视频游戏,其中倾斜是玩家的主要控制方法。
但我希望玩家即使躺在沙发上也能玩游戏。
目前,如果设备是平的,游戏运行效果最好,如果你稍微倾斜一点,游戏仍然可以运行,因为我计算了加速度计的起点。但这会产生意想不到的结果。
Android 中有没有一种方法可以计算设备从特定起点的旋转(度数)?甚至有可能吗?有人能够做到这一点吗?
我知道 SpeedX 可以控制旋转,但我需要它来倾斜。
谢谢
您可以获得有关 angular(旋转)速度的持续回调。将其转换为 angular 位置取决于您。 [1]
[1] http://developer.android.com/guide/topics/sensors/sensors_motion.html#sensors-motion-gyro
您可以通过
获取设备旋转度数
OrientationEventListener.onOrientationChanged()
范围从 0 到 359 度,但您必须自己计算起点和旋转角度变化之间的差异。
void onOrientationChanged (int orientation) {
//orientation is an argument which represents rotation in degrees
}
您也可以通过调用侦听器的 enable()
和 disable()
方法来启用和禁用此侦听器。这是来自 google.
的 deveoper reference docs
this 是关于如何使用定向侦听器的 link。
public class SimpleOrientationActivity extends Activity {
OrientationEventListener mOrientationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOrientationListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
Log.v(DEBUG_TAG, "Orientation changed to " + orientation);
}
};
if (mOrientationListener.canDetectOrientation() == true) {
Log.v(DEBUG_TAG, "Can detect orientation");
mOrientationListener.enable();
} else {
Log.v(DEBUG_TAG, "Cannot detect orientation");
mOrientationListener.disable();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mOrientationListener.disable();
}
}
还有适合游戏使用和其他目的的其他速率值。默认速率 SENSOR_DELAY_NORMAL 最适合简单的方向更改。其他值,例如 SENSOR_DELAY_UI 和 SENSOR_DELAY_GAME 可能适合您。
This 是另一个有用的 link,它实现了相同的代码并提供了很好的解释。
我正在开发一款视频游戏,其中倾斜是玩家的主要控制方法。 但我希望玩家即使躺在沙发上也能玩游戏。 目前,如果设备是平的,游戏运行效果最好,如果你稍微倾斜一点,游戏仍然可以运行,因为我计算了加速度计的起点。但这会产生意想不到的结果。
Android 中有没有一种方法可以计算设备从特定起点的旋转(度数)?甚至有可能吗?有人能够做到这一点吗? 我知道 SpeedX 可以控制旋转,但我需要它来倾斜。
谢谢
您可以获得有关 angular(旋转)速度的持续回调。将其转换为 angular 位置取决于您。 [1]
[1] http://developer.android.com/guide/topics/sensors/sensors_motion.html#sensors-motion-gyro
您可以通过
获取设备旋转度数OrientationEventListener.onOrientationChanged()
范围从 0 到 359 度,但您必须自己计算起点和旋转角度变化之间的差异。
void onOrientationChanged (int orientation) {
//orientation is an argument which represents rotation in degrees
}
您也可以通过调用侦听器的 enable()
和 disable()
方法来启用和禁用此侦听器。这是来自 google.
this 是关于如何使用定向侦听器的 link。
public class SimpleOrientationActivity extends Activity {
OrientationEventListener mOrientationListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOrientationListener = new OrientationEventListener(this,
SensorManager.SENSOR_DELAY_NORMAL) {
@Override
public void onOrientationChanged(int orientation) {
Log.v(DEBUG_TAG, "Orientation changed to " + orientation);
}
};
if (mOrientationListener.canDetectOrientation() == true) {
Log.v(DEBUG_TAG, "Can detect orientation");
mOrientationListener.enable();
} else {
Log.v(DEBUG_TAG, "Cannot detect orientation");
mOrientationListener.disable();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mOrientationListener.disable();
}
}
还有适合游戏使用和其他目的的其他速率值。默认速率 SENSOR_DELAY_NORMAL 最适合简单的方向更改。其他值,例如 SENSOR_DELAY_UI 和 SENSOR_DELAY_GAME 可能适合您。
This 是另一个有用的 link,它实现了相同的代码并提供了很好的解释。