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

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

问题描述

我的游戏使用加速度计,一切工作正常,但我注意到有将是一些手机有轻微的问题。本场比赛迫使方向为横向,但有些手机有景观的默认屏幕方向......

现在,我总是假设用户将不得不倾斜他们的电话,以正确地看到比赛,这将意味着加速度计读数X轴将移动玩家在Y和Y读会移动播放器在十对于具有默认横向屏幕的X将对应于游戏和X在Y对Y在游戏中的手机(如他们不会需要倾斜他们的电话。)

我的问题是我怎么处理呢?我能适应code反转基于手机定位的X和Y方向,所以我将如何得到手机编程当前的方向?如果有更好的方法,这将是巨大的。

希望是有道理的。

编辑:

其实会是安全的假设,如果这些设备的长度大于其高度的屏幕将被默认为景观?

解决方案

 显示显示=((窗口管理器)getSystemService(WINDOW_SERVICE))getDefaultDisplay()。
INT旋转= display.getRotation();
 

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

  

公众诠释getRotation()

     

返回其自然屏幕方向旋转。该   返回值可以为Surface.ROTATION_0(无旋转),   Surface.ROTATION_90,Surface.ROTATION_180,或Surface.ROTATION_270。   例如,如果一个设备具有自然高大的屏幕,并且用户具有   把它在其一侧进入一个横向的价值   回到这里可能是Surface.ROTATION_90或   Surface.ROTATION_270取决于它转动的方向。该   角是绘制图形的屏幕上,这是在转动   该装置的物理旋转方向相反。对于   例如,如果该装置被旋转90度逆时针,以   补偿渲染将90度顺时针并且因此转动   返回值,这里将成为Surface.ROTATION_90。

您将不得不实验用的设备,以确保机器人正确地知道了自然的方向。如果是这样,那么就检查ROTATION_90(或ROTATION_270),看看该设备是在它的侧面或没有。如果是,则切换x和y轴。

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...

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.)

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

Hope that makes sense

EDIT:

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();

from http://developer.android.com/reference/android/view/Display.html:

public int getRotation ()

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.

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天全站免登陆