如何拦截Android的倾斜? [英] How to intercept tilting in Android?

查看:170
本文介绍了如何拦截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?

推荐答案

主要的事情让你的头周围的侦听器的概念。
在Android中没有一个调用方法 getXtilt() getYtilt()等来获得方向。
相反,你需要创建一个监听器您的系统中注册。

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.

这个

查看 onSensorChanged(SensorEvent事件)的方法? Android系统将每次调用该方法,传感器的变化(这是很频繁)。在这种情况下,这将是您将收到的 TYPE_ACCELEROMETER 传感器读数。

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 the TYPE_ACCELEROMETER sensor readings you will be receiving.

所以,当你拿到SensorEvent 事件看看在 event.values​​ [] 阵列。它将包含传感器读数。在Android的文档的例子code,他们注册 Sensor.TYPE_ACCELEROMETER 。你应该注册
Sensor.TYPE_ORIENTATION
。看一看在阵列
Sensor.TYPE_ORIENTATION 。他们是你正在寻找的倾斜值。

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 the Sensor.TYPE_ACCELEROMETER. You should register the Sensor.TYPE_ORIENTATION. Have a look at the values array for Sensor.TYPE_ORIENTATION. They are the tilt values you are looking for.

希望帮助

这篇关于如何拦截Android的倾斜?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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