The use of the sensor in the direction of Android development(2011-10-13 20:56:05)
reproduced
Tags: android sensor direction Compass talk |
Category: 3GAndroid Learning Notes |
The orientation sensor in Android is a good application in life, the typical example is the use of the compass, let us briefly introduce the three parameters of the sensor x, y, z meaning, with a picture to illustrate.
Figure 1Additional note: The axes of x, y, z and z in the figure have no connection! As shown, the green part represents a cell phone with a small circle that is the x in the head sensor of the phone: as shown, the x positive half axis is north, the phone head points to the direction of the, at this time the value of x is 0, If the phone head points to the OG direction, this time the X value is 90, point to Oh direction, x value is 180, point to oe,x value is 270
Y in the sensor: now we move the phone along the BC axis slowly upward, that is, the phone head does not move, the tail slowly upward up, until the ad ran to the right of BC and landed on the Xoy plane, Y value will change from the 0~180, if the cell phone along the ad axis slowly upward, that the phone tail does not understand, Until BC runs to the left of AD and falls on the Xoy plane, the value of Y will change from 0~-180, which is the meaning of y in the direction sensor.
Z in sensor: Now we move the phone along the AB axis slowly upward, that is, the left frame of the mobile phone, the right frame slowly upward up, until the CD ran to the right of AB and landed on the Xoy plane, Z value will change from the 0~180, if the phone along the CD axis slowly upward, that the mobile phone right frame is not moving, Until AB runs to the left of the CD and falls on the Xoy plane, the value of Z will change from 0~-180, which is the meaning of z in the direction sensor.
Understand the meaning of x, Y, z in the direction sensor after we start to learn how to use the first we create a sensor manager and a sensor listener, the manager is used to manage the sensor and create a variety of sensors, the listener is used to monitor the sensor changes and the corresponding operation of the private Sensormanager sensormanager;private Mysensoreventlistener mysensoreventlistener;mysensoreventlistener= New Mysensoreventlistener ();//The listener is, of course, our own definition, in the direction of the sensor sensor to change the direction of the mobile phone, we can take the appropriate action, here is the value of x, Y and Z is printed out the private final class Mysensoreventlistener Implementssensoreventlistener{
@Override//Can be obtained real-time measurement of the sensor change value public void onsensorchanged (Sensorevent event) {//Direction sensor if (event.sensor.getType () = = Sensor.type_orientation) {//x represents the direction the phone points to, 0 for north, 90 for east, 180 for South, and 270 for West float x = event.values[sensormanager.data_x];float y = Event.values[sensormanager.data_y];float Z = event.values[sensormanager.data_z];//tv_ Orientation is a TextView tag on the interface, no longer repeating tv_orientation.settext ("Orientation:" +x+ "," +y+ "," +z ");}
We create a directional sensor in the Onresume method and register the listener with the system protected void Onresume () {Sensor sensor_orientation=sensormanager.getdefaultsensor (sensor.type_orientation);Sensormanager.registerlistener (Mysensoreventlistener,sensor_orientation, sensormanager.sensor_delay_ui); Super.onresume ();} Finally we log off all sensor monitors in OnPause (), releasing the direction sensor resource!protected void OnPause () {//logging off all sensor monitoring Sensormanager.unregisterlistener ( Mysensoreventlistener); Super.onpause ();}
To this, the introduction of the direction of the sensor is complete!
For the source of Eclipse engineering documents for this course, click here to download
Source: http://blog.sina.com.cn/s/blog_5a48dd2d0100u4lx.html
Null
The direction of Android development sensor use? (2011-10-13 20:56:05) reprinted