开发者

How to intercept tilting in Android?

开发者 https://www.devze.com 2023-02-15 02:03 出处:网络
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开发者_Go百科 the tilt?The main thing

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开发者_Go百科 the tilt?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消