简体   繁体   English

如何检测手机倾斜?

[英]How to detect phone tilt?

How to determine the angle of the phone orientation in the one plane? 如何确定一个平面中手机方向的角度?

Now I make it through SensorManager: 现在我通过SensorManager实现:

mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);

...

public void onSensorChanged(SensorEvent event) {
    xy_angle = event.values[0]; 
    xz_angle = event.values[1]; 
    zy_angle = event.values[2]; 

Here I get different angles, but I need only one angle, which varies with the rotation of the phone, while driving along the blue line. 在这里我得到不同的角度,但我只需要一个角度,随着手机的旋转而变化,同时沿着蓝线行驶。

How calc or how get this angle? 如何计算或如何得到这个角度?

http://i.stack.imgur.com/kCElj.jpg

How can I use SensorManager.getOrientation for tilt controls like "My Paper Plane"? 如何将SensorManager.getOrientation用于“My Paper Plane”等倾斜控件? - similar question here, but I don't understand how author solve his problem. - 这里有类似的问题,但我不明白作者如何解决他的问题。

您需要的是SensorManager.getOrientation()

I see that this post is a bit old, so maybe it's been answered by now, but Sensor.TYPE_ORIENTATION is now deprecated in favor of SensorManager.getOrientation() . 我看到这篇文章有点陈旧,所以现在可能已经回答了,但现在不推荐使用Sensor.TYPE_ORIENTATION来支持SensorManager.getOrientation()

I've done a full writeup of essentially what you're looking for here: http://blog.samsandberg.com/2015/05/10/tilt-for-android/ 我已完成了基本上你正在寻找的内容的完整文章: http//blog.samsandberg.com/2015/05/10/tilt-for-android/

Hope it helps someone looking for the same thing! 希望它可以帮助某人寻找同样的事情!

The author of that question solved his problem by switching his parameter order: 该问题的作者通过切换参数顺序解决了他的问题:

 if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                    m_lastMagFields, m_lastAccels)) {

to: 至:

if (SensorManager.getRotationMatrix(m_rotationMatrix, null,
                                m_lastAccels, m_lastMagFields)) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM