Android中如何拦截倾斜?
我想在android中编写一个使用倾斜功能的程序。
有什么办法拦截吗?我能得到什么回报?表示倾斜方向的向量?
I would like to write a program using tilting functionality in android.
Is there any way to intercept it? What do I get back? a vector indicating the direction of the tilt?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最重要的是要了解听众的概念。
在 Android 中,没有名为
getXtilt()
、getYtilt()
等的方法来获取方向。相反,您需要创建一个在系统中注册的侦听器。
看看这个。
看到
onSensorChanged(SensorEvent event)
方法了吗?每次传感器发生变化时(这种情况非常频繁),Android 系统都会调用该方法。在这种情况下,您将收到的将是TYPE_ACCELEROMETER
传感器读数。因此,当您获取 SensorEvent 'event' 时,请查看 < code>event.values[] 数组。它将包含传感器读数。在 Android 文档的示例代码中,他们注册了
Sensor.TYPE_ACCELEROMETER
。您应该注册
。查看 values 数组传感器.TYPE_ORIENTATION
传感器.TYPE_ORIENTATION
。它们是您正在寻找的倾斜值。希望有帮助
The main thing to get your head around it the concept of a listener.
In Android there isn't a method called
getXtilt()
,getYtilt()
etc to get the orientation.Instead you need to create a listener which you register with the system.
Look at this.
See the
onSensorChanged(SensorEvent event)
method? The Android system will invoke that method every time the sensor changes (which is very frequently). In this case it will be theTYPE_ACCELEROMETER
sensor readings you will be receiving.So when you get the SensorEvent 'event' have a look at the
event.values[]
array. It will contain the sensor readings. In the example code in the Android doc they register theSensor.TYPE_ACCELEROMETER
. You should register the
. Have a look at the values array forSensor.TYPE_ORIENTATION
Sensor.TYPE_ORIENTATION
. They are the tilt values you are looking for.hope that helps
听起来您必须使用加速度计并解释这些值,然后根据它们做出决定。查找 SensorEventListener,它应该会让您找到正确的方向。
Sounds like you would have to use the Accelerometer and interpret the values and then make a decision based on them. Look up SensorEventListener and it should get you in the right direction.