Detailed explanation of definition and usage of direction sensor in Android Development [compass implementation method attached]

This paper describes the definition and usage of direction sensor in Android development. Share with you for your reference, as follows:

The direction sensor in Android is a good application in life. A typical example is the use of compass. Let's briefly introduce the meaning of the three parameters x, y and Z in the sensor and illustrate it with a picture.

Supplementary note: the coordinate axes X and Z in the figure have no relationship with X, y and Z in the sensor!

As shown in the above figure, the green part represents a mobile phone, and the end with a small circle is the mobile phone head

X in the sensor: as shown in the above figure, it is specified that the positive half axis of X is north, the mobile phone head points to the direction of of, and the value of X is 0. If the mobile phone head points to the direction of og, the value of X is 90, pointing to the direction of Oh, the value of X is 180, pointing to OE, and the value of X is 270

Y in the sensor: now we slowly lift the mobile phone up along the BC axis, that is, the head of the mobile phone does not move, and the tail slowly tilts up until ad runs to the right of BC and falls on the xoy plane, and the value of Y will change from 0 to 180. If the mobile phone slowly lifts up along the ad axis, that is, the tail of the mobile phone does not understand until BC runs to the left of AD and falls on the xoy plane, The value of Y will vary from 0 to 180, which is the meaning of Y in the direction sensor.

Z in the sensor: now we slowly lift the mobile phone up along the AB axis, that is, the left frame of the mobile phone does not move, and the right frame slowly tilts up until the CD runs to the right of AB and falls on the xoy plane, and the value of Z will change from 0 to 180. If the mobile phone slowly lifts up along the CD axis, that is, the right frame of the mobile phone does not move until AB runs to the left of the CD and falls on the xoy plane, The value of Z will vary from 0 to 180, which is the meaning of sending Z in the direction sensor.

After understanding the meaning of X, y and Z in the direction sensor, let's start to learn how to use it

First, we create a sensor manager and a sensor listener. The manager is used to manage sensors and create various sensors. The listener is used to monitor the changes of sensors and perform corresponding operations

We create a direction sensor in the onresume method and register the listener with the system

Finally, we log off the monitoring of all sensors in onpause() to release the direction sensor resources!

This completes the introduction of the direction sensor!

Click here to download the complete example code.

Attachment: Android realizes compass function based on direction sensor

Step 1: create a new project compass, and import a compass picture into the RES / drawable hdpi directory

Step 2: design the UI interface of the application, main.xml

step3:MainActivity.java

step4:AndroidManifest.xml

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android view skills, summary of activity operation skills of Android programming, summary of Android operation skills of SQLite database, summary of Android operation skills of JSON format data, summary of Android resource operation skills, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>