Android uses sensor to realize the small function of compass

First, let's talk about the implementation idea of the compass:

The program first prepares a compass picture, and the direction pointer on the picture points to the north. Next, develop a sensor to detect the direction. The program detects how many degrees the top of the mobile phone rotates around the Z axis and how many degrees the compass picture rotates in the opposite direction. It can be seen that the compass application only needs to add a picture in the interface and make the picture always turn around the first angle value returned by the reverse sensor.

Here is the direction sensor: the direction sensor is used to sense the placement status of mobile phone devices. The direction sensor can return three angles, which can determine the placement status of the mobile phone. The three angles returned by the direction sensor are described below.

The first angle: indicates the included angle of the top of the mobile phone towards the north. When the phone rotates around the Z axis, the angle value changes. For example, when the angle is 0, it indicates that the top of the mobile phone faces due north; When the angle is 90, it means that the top of the mobile phone faces due east; When the angle is 180, it means that the top of the mobile phone faces due south; When the angle is 270, it means that the top of the mobile phone faces due west.

Second angle: indicates the angle at which the top or tail of the phone is tilted. When the phone tilts around the X axis, the angle value changes. The value range of this angle is - 180 ~ 180. Suppose that the mobile phone screen is placed horizontally on the table. If the table is completely horizontal, the angle value should be 0. Lift the mobile phone from the top until the mobile phone rotates 180 degrees along the X axis (the screen is placed horizontally on the desktop). During this rotation, the angle value will change from 0 to - 180. That is, when lifting from the top of the mobile phone, the angle value will gradually decrease until it is equal to - 180; If you lift up from the bottom of the phone and know to rotate the phone 180 degrees along the X axis (the screen is placed horizontally down on the desktop), the angle value will change from 0 to 180. That is, when lifting from the top of the phone, the angle value will gradually increase until it is equal to 180.

The third angle: indicates the angle at which the left or right side of the phone is tilted. When the phone tilts around the Y axis, the angle value changes. The value range of this angle is - 90 ~ 90. Suppose that the mobile phone screen is placed horizontally on the table. If the table is completely horizontal, the angle value should be 0. Suppose you gradually lift the left side of the mobile phone and know that you can rotate the mobile phone 90 degrees along the Y axis (the mobile phone is perpendicular to the desktop). During this rotation, the angle value will change from 0 to - 90. That is, when lifting from the left side of the mobile phone, the angle value will gradually decrease until it is equal to - 90; If you gradually lift the right side of the mobile phone, you know to rotate the mobile phone 90 degrees along the Y axis (the mobile phone is perpendicular to the desktop). During this rotation, the angle value will change from 0 to 90. In other words, when lifting from the right side of the phone, the angle value will gradually increase until it is equal to 90.

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

compass.png

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

step3:MainActivity.java

step4:AndroidManifest.xml

Step5: you can view the running effect by deploying the application to the real machine environment!

My android advanced journey ------ > a small example of Android using sensor to realize the function of level gauge

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>