【问题标题】:Device tilting degrees on AndroidAndroid上的设备倾斜度
【发布时间】:2016-04-18 12:49:14
【问题描述】:

我正在开发一款视频游戏,其中倾斜是玩家的主要控制方法。 但我希望玩家即使躺在沙发上也能玩游戏。 目前,如果设备是平的,游戏效果最好,如果你稍微倾斜一点,游戏仍然可以工作,因为我计算了加速度计的起点。但这会产生意想不到的结果。

Android 中有没有一种方法可以从特定起点计算设备的旋转(度数)?甚至有可能吗?有没有人能够做到这一点? 我知道 SpeedX 能够掌握旋转,但我需要它来倾斜。

谢谢

【问题讨论】:

  • 可以做类似的事情,当你开始游戏时,让用户通知你将校准他们的设备,然后获取设备的当前倾斜并将其存储到startingTilt。然后,每当您获得他们设备的倾斜度时,只需执行 (currentTilt-startingTilt)。基本上,在游戏开始时,获取他们的倾斜度并管理与该起始倾斜度相关的后续输入。

标签: android android-sensors


【解决方案1】:

您可以获得关于角(旋转)速度的持续回调。您可以将其转换为角度位置。 [1]

[1]http://developer.android.com/guide/topics/sensors/sensors_motion.html#sensors-motion-gyro

【讨论】:

【解决方案2】:

可以通过

获取设备旋转度数
OrientationEventListener.onOrientationChanged() 

范围从 0 到 359 度,但您必须自己计算起点和旋转度数变化之间的差异。

void onOrientationChanged (int orientation) { 
    //orientation is an argument which represents rotation in degrees
}

您也可以通过调用侦听器的enable() 和disable() 方法来启用和禁用此侦听器。这是来自谷歌的deveoper reference docs。

this 是一个关于如何使用方向监听器的链接。

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 是另一个有用的链接,它实现了相同的代码并提供了很好的解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2014-03-11
    • 2023-03-23
    • 2017-05-18
    • 2016-07-07
    相关资源
    最近更新 更多