Android 加速度计问题 [英] Android Accelerometer Issue

查看:27
本文介绍了Android 加速度计问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏使用加速度计,一切正常,但我注意到某些手机会出现小问题.游戏强制方向为横向,但有些手机的默认屏幕方向为横向...

My game uses the accelerometer, everything works fine but I noticed there is going to be a slight issue with some phones. The game forces the orientation to be landscape however some phones have a default screen orientation of landscape...

现在我一直假设用户将不得不倾斜他们的手机才能正确观看游戏,这意味着 X 轴的加速度计读数会在 Y 轴上移动玩家,而 Y 轴读数会移动玩家在 X 中.对于具有默认横向屏幕的手机,X 将对应于游戏中的 X,而 Y 对应于游戏中的 Y(因为他们不需要倾斜手机.)

Right now I was always assuming the user was going to have to tilt their phone to see the game correctly, this would mean the accelerometer reading for the X axis would move the player in the Y and the Y reading would move the player in the X. With the phones that have a default landscape screen the X will correspond to the X in game and the Y for the Y in game (as they wont need to tilt their phones.)

我的问题是我该如何处理?我可以修改代码以根据手机方向反转 X 和 Y 方向,那么我将如何以编程方式获得手机的当前方向?如果有更好的方法就好了

My question is how do I handle this? I can adapt the code to invert the X and Y directions based on the phones orientation, so how would I get the current orientation of the phone programmatically? If there is a better way that would be great

希望有意义

实际上是否可以安全地假设,如果设备长度大于其高度,屏幕将默认为横向?

Actually would it be safe to assume that if the devices length is greater than its height the screen would be defaulted to landscape?

推荐答案

Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();

来自 http://developer.android.com/reference/android/view/Display.html:

public int getRotation()

public int getRotation ()

从自然"方向返回屏幕的旋转.这返回值可能是 Surface.ROTATION_0(无旋转),Surface.ROTATION_90、Surface.ROTATION_180 或 Surface.ROTATION_270.例如,如果一个设备有一个自然高的屏幕,并且用户有把它翻过来变成横向,价值此处返回的可能是 Surface.ROTATION_90 或Surface.ROTATION_270 取决于它转动的方向.这角度是绘制图形在屏幕上的旋转,即设备物理旋转的相反方向.为了例如,如果设备逆时针旋转 90 度,补偿渲染将顺时针旋转 90 度,因此此处返回的值为 Surface.ROTATION_90.

Returns the rotation of the screen from its "natural" orientation. The returned value may be Surface.ROTATION_0 (no rotation), Surface.ROTATION_90, Surface.ROTATION_180, or Surface.ROTATION_270. For example, if a device has a naturally tall screen, and the user has turned it on its side to go into a landscape orientation, the value returned here may be either Surface.ROTATION_90 or Surface.ROTATION_270 depending on the direction it was turned. The angle is the rotation of the drawn graphics on the screen, which is the opposite direction of the physical rotation of the device. For example, if the device is rotated 90 degrees counter-clockwise, to compensate rendering will be rotated by 90 degrees clockwise and thus the returned value here will be Surface.ROTATION_90.

您必须对设备进行试验,以确保 android 正确了解自然"方向.如果是这样,那么只需检查 ROTATION_90(或 ROTATION_270)以查看设备是否在其侧面.如果是,则切换 x 和 y 轴.

You'll have to experiment with the devices to make sure android knows the "natural" orientation correctly. If so, then just check for ROTATION_90 (or ROTATION_270) to see if the device is on it's side or not. If so, then switch x and y axis.

这篇关于Android 加速度计问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆