Gravity sensing usage analysis of Android programming

This paper illustrates the gravity sensing usage of Android programming. Share with you for your reference, as follows:

Gravity sensing is mainly realized by the acceleration sensor of the mobile phone

There are eight kinds of sensors in the development of Android, but not necessarily every real machine supports these sensors. Because many function users don't care at all, developers may block some functions. It still needs to be developed according to the actual situation of the real machine. The specific implementation method of the acceleration sensor is mainly discussed below. The name of the sensor is as follows:

Accelerometer gyroscope ambient light magnetic field orientation pressure proximity temperature

1. Sensormanager sensor management object

All sensors in the mobile phone need to be accessed through sensormanager. You can get the sensor management object of the current mobile phone by calling the getsystemservice (sensor_service) method.

2. Implement the sensoreventlistener interface

We need to implement the sensoreventlistener interface onsensorchanged (sensoreventevent) method to capture the state of the mobile phone sensor and get the gravity components in the x-axis, Y-axis and z-axis of the mobile phone. With the data in these three directions, we have learned the principle of gravity induction.

As shown in the above code, the value range of float X Y Z is between - 10 and 10. Let's explain the meaning of x-axis, Y-axis and z-axis gravity component (it should be noted here that the coordinate origin: positive to the sky and negative to the ground, just opposite to the coordinates during programming):

(1) On the left side of the mobile phone screen, the X axis faces the sky and is placed vertically. At this time, the Y axis and Z axis have no gravity component. Because the X axis faces the sky, its gravity component is the largest. At this time, the values of gravity components of X axis, Y axis and Z axis are (10, 0, 0) respectively;

(2) On the right side of the mobile phone screen, the X axis faces the ground and is placed vertically. At this time, the Y axis and Z axis have no gravity component. Because the X axis faces the ground, its gravity component is the smallest. At this time, the values of gravity components of X axis, Y axis and Z axis are (- 10, 0, 0) respectively;

(3) The mobile phone screen is placed vertically, and the Y axis faces the sky. At this time, the X axis and Z axis have no gravity component, because the Y axis faces the sky, its gravity component is the largest. At this time, the values of gravity components of X axis, Y axis and Z axis are (0, 10, 0) respectively;

(4) The mobile phone screen is placed vertically, and the Y axis faces the ground. At this time, the X axis and Z axis have no gravity component, because the Y axis faces the ground, its gravity component is the smallest. At this time, the values of gravity components of X axis, Y axis and Z axis are (0, - 10, 0) respectively;

(5) When the mobile phone screen is upward, the Z axis faces the sky and is placed horizontally. At this time, the X axis and Y axis have no gravity component. Because the Z axis faces the sky, its gravity component is the largest. At this time, the values of gravity components of X axis, Y axis and Z axis are (0, 0 and 10) respectively;

(6) When the mobile phone screen is upward, the Z axis faces the ground and is placed horizontally. At this time, the X axis and Y axis have no gravity component. Because the Z axis faces the ground, its gravity component is the smallest. At this time, the values of gravity components of X axis, Y axis and Z axis are (0, 0, - 10) respectively.

3. Register sensoreventlistener

Use sensormannager to call the getdefaultsensor (sensor. Type_accelerometer) method to get the sensor object of acceleration gravity sensing. Because I'm talking about gravity acceleration sensor, the parameter is sensor TYPE_ Accelerometer, if you need to get other sensors, you need to pass in the corresponding name. Use sensormannager to call the registerlistener () method to register. The third parameter is the sensitivity and accuracy of detection. The accuracy can be selected according to different needs. It is recommended to use sensormanagersensor for game development_ DELAY_ GAME。

4 . Simple velocity calculation method of gravity induction

Each time you shake the mobile phone, you can calculate the gravity component of X axis, Y axis and Z axis, and record them. Then, you can compare the gravity component of each shaking with the previous gravity component, and use the difference and time to calculate their moving speed.

The gravity sensing device includes three parts: sensor, processor and controller. The sensor is responsible for detecting the state of the memory and calculating the gravity acceleration value of the memory; The processor judges whether the acceleration value exceeds the safe range; The controller is responsible for controlling to lock or release the magnetic head out of the safe parking area. Once the sensor detects and the processor judges that the current gravity acceleration exceeds the safe value, the controller will control the magnetic head to stop reading and writing through hardware, quickly return and lock it in the exclusive magnetic head parking area. This series of actions will be completed in 200 milliseconds. When the sensing device detects that the acceleration value returns to the normal value range, the product will resume operation.

The code of Android multimedia framework is in the following directory: external / OpenCORE / This directory is the root directory of Android multimedia framework, and its subdirectories are as follows:

*Android: This is an upper-level library. It implements a player and author * baselibs for Android based on the SDK of pvplayer and pvauthor * baselibs: a lower-level library * codecs containing data structure and thread safety_ V2: This is a library with many contents, mainly including the implementation of codec and an implementation of openmax * engines: including the implementation of pvplayer and pvauthor engine * extern_ libs_ V2: contains the header file of Khronos's openmax * fileformats: parser class of file format * nodes: node classes of encoding, decoding and file parsing * OsCl: operating system compatibility library * pvmi: abstract interface of input / output control * protocols: mainly the contents of network related RTSP, RTP, HTTP and other protocols * pvcommon: Android of pvcommon library files MK file, Android without source file * pvplayer: pvplayer library file MK file, no source file * pvauthor: Android of pvauthor library file MK file, no source file * tools_ V2: compilation tools and some registrable modules

The following are some test codes:

For more Android related content, readers who are interested 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 skills of operating SQLite database for Android, summary of skills of operating JSON format data for Android, summary of operating skills of Android database Summary of Android file operation skills, summary of SD card operation methods for Android programming development, summary of Android resource operation skills and summary of Android control usage

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