Detailed explanation of gravity sensor usage examples in Android Development

This article illustrates the usage of gravity sensor in Android development. Share with you for your reference, as follows:

The development steps of gravity sensor and direction sensor are similar. As long as the values of X, y and Z in the period are sorted out, they can be programmed according to their changes. First, let's take a look at a picture

Suppose the local gravitational acceleration value is g, when the front of the mobile phone is upward, the value of Z is Q, when the back is upward, the value of Z is - G, when the right side of the mobile phone is upward, the value of X is g, when the right side is upward, the value of X is - G, when the top side of the mobile phone is upward, the value of Y is g, and the value of Y is - G

After understanding the meaning of X, y and Z in the gravity 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 the gravity 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 gravity sensor resources!

This completes the introduction of the gravity sensor!

Next, let's take a look at an example of Android using gravity sensors to switch between horizontal and vertical screens

When playing a video, you may have to switch between horizontal and vertical screens. However, users can set their mobile phone to turn off the screen rotation. At this time, you need to think of other ways, such as gravity sensor.

When using:

After calling activity. Setrequestedorientation(), activity. Onconfigurationchanged() will be triggered; You can reset the size of the playback interface here.

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