How to intercept tilting in Android?

谁都会走 提交于 2020-01-17 04:36:51

问题


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?


回答1:


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

hope that helps




回答2:


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.



来源:https://stackoverflow.com/questions/5199132/how-to-intercept-tilting-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!