我的代码如下:
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";
}以上代码(内部渲染方法)总是指示设备处于纵向模式,即使当我旋转设备时!我做错了什么?我如何准确地检测设备是在肖像模式还是景观模式?
发布于 2016-10-04 23:01:51
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");https://stackoverflow.com/questions/39862095
复制相似问题