【问题标题】:Orientation always result to portrait (Motion Controls in libgdx)方向总是导致纵向(libgdx 中的运动控制)
【发布时间】:2016-10-04 21:31:59
【问题描述】:
我的代码如下:
Input.Orientation orientation = Gdx.input.getNativeOrientation();
message = "";
switch (orientation) {
case Landscape:
message += "Landscape\n";
break;
case Portrait:
message += "Portrait\n";
break;
default:
message += "Whatever\n";
}
上面的代码(内部渲染方法)总是表明设备处于纵向模式,即使我旋转设备!我究竟做错了什么?如何准确检测设备是纵向模式还是横向模式?
【问题讨论】:
标签:
java
android
libgdx
orientation
【解决方案1】:
getNativeOrientation() 不会返回设备的当前方向,当您正确握住它时,它会返回屏幕是横向还是纵向的东西(在大多数手机上它应该返回纵向,但我猜在平板电脑和手机上HTC ChaCha 它返回横向)。
有几种方法可以获取当前方向:
-
推荐:使用
Gdx.input.getRotation(),它返回设备相对于其原始方向的旋转角度(0、90、180、270)。将它与getNativeOrientation() 一起使用,您应该能够为所有设备获得正确的方向。
注意:它为您提供应用程序当前状态的方向,而不是设备作为物理对象的方向。因此,如果您的清单中有 android:screenOrientation="landscape",此方法将始终返回横向。
示例用法:
int rotation = Gdx.input.getRotation();
if((Gdx.input.getNativeOrientation() == Input.Orientation.Portrait && (rotation == 90 || rotation == 270)) || //First case, the normal phone
(Gdx.input.getNativeOrientation() == Input.Orientation.Landscape && (rotation == 0 || rotation == 180))) //Second case, the landscape device
Gdx.app.log("Orientation", "We are in landscape!");
else
Gdx.app.log("Orientation", "We are in portrait");