English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article illustrates the application method of gravity sensing in Android programming. Shared for everyone's reference, as follows:
Gravity sensing is mainly realized by the accelerometer of the phone
In Android development, there are a total of eight sensors, but not every real device supports these sensors. Since many functions are not cared about by users, developers may disable some features. Development should still be based on the actual situation of the real device. The following mainly discusses the specific implementation methods of the accelerometer, with the sensor names as follows:
Accelerometer sensor (accelerometer)
Gyroscope sensor (gyroscope)
Ambient light sensor (light)
Magnetic field sensor (magnetic field)
Orientation sensor (orientation)
Pressure sensor (pressure)
Proximity sensor (proximity)
Temperature sensor (temperature)
1. SensorManager sensor management object
All sensors in the phone must be accessed through the SensorManager, and the getSystemService(SENSOR_SERVICE) method can be called to obtain the current sensor management object of the phone.
2. Implement the SensorEventListener interface
We need to implement the SensorEventListener interface and the onSensorChanged(SensorEvent event) method to capture the state of the phone sensor, obtain the gravitational components of the X-axis, Y-axis, and Z-axis in three directions of the phone, and with these three directional data, we have already learned the principle of gravity sensing.
public void onSensorChanged(SensorEvent e) { float x = e.values[SensorManager.DATA_X]; float y = e.values[SensorManager.DATA_Y]; float z = e.values[SensorManager.DATA_Z]; };
As shown in the above code: float x y z 3The range of values for each of the above directions is-10to10Here, the meaning of the gravitational components of the X-axis, Y-axis, and Z-axis is explained (note that the coordinate origin: upwards towards the sky is a positive number, downwards towards the ground is a negative number, which is exactly opposite to the coordinates used in programming):
(1The phone screen is facing to the left, with the X-axis pointing towards the sky. When placed vertically, the Y-axis and Z-axis have no gravitational components, as the X-axis is pointing towards the sky, so its gravitational component is the largest. At this time, the values of the gravitational components of the X-axis, Y-axis, and Z-axis are (100, 0);
(2The phone screen is facing to the right, with the X-axis pointing towards the ground. When placed vertically, the Y-axis and Z-axis have no gravitational components, as the X-axis is pointing towards the ground, so its gravitational component is the smallest. At this time, the values of the gravitational components of the X-axis, Y-axis, and Z-axis are (-100, 0);
(3The phone screen is vertically upright, with the Y-axis pointing towards the sky. When placed vertically, the X-axis and Z-axis have no gravitational components, as the Y-axis is pointing towards the sky, so its gravitational component is the largest. At this time, the values of the gravitational components of the X-axis, Y-axis, and Z-axis are (0,100);
(4The phone screen is vertically upright, with the Y-axis pointing towards the ground. When placed vertically, the X-axis and Z-axis have no gravitational components, as the Y-axis is pointing towards the ground, so its gravitational component is the smallest. At this time, the values of the gravitational components of the X-axis, Y-axis, and Z-axis are (0,-100);
(5The phone screen is facing upwards, with the Z-axis pointing towards the sky. When placed horizontally, the X-axis and Y-axis have no gravitational components, as the Z-axis is pointing towards the sky, so its gravitational component is the largest. At this time, the values of the gravitational components of the X-axis, Y-axis, and Z-axis are (0, 0,10);
(6)(The phone screen is upward, the Z-axis points to the ground when it is horizontally placed, at this time, the X-axis and Y-axis have no gravity components because the Z-axis points to the ground, so its gravity component is the smallest. At this time, the values of the gravity components of the X-axis, Y-axis, and Z-axis are (0, 0,-10)
3. Register SensorEventListener
Use the SensorManager to call the getDefaultSensor(Sensor.TYPE_ACCELEROMETER) method to obtain the Sensor object for the acceleration gravity sensing. Since I am discussing the gravity acceleration sensor, the parameter is Sensor.TYPE_ACCELEROMETER. If you need to obtain other sensors, you need to pass in the corresponding name. Use SensorManager to call the registerListener() method to register, the third parameter is the detection sensitivity accuracy, choose the accuracy according to different needs, and game development is recommended to use SensorManagerSENSOR_DELAY_GAME.
4 . This is a simple way to calculate the speed using gravity sensing
Each time the phone is shaken, the gravity components of the X-axis, Y-axis, and Z-axis can be calculated and recorded. Then, by comparing the gravity components of each shake with the previous ones, the speed of their movement can be calculated using the difference and time.
The gravity sensing device includes three parts: sensors, processors, and controllers. Sensors are responsible for detecting the status of the storage, calculating the gravity acceleration value of the storage; processors judge whether the acceleration value exceeds the safe range; and the controller is responsible for controlling the locking or releasing of the magnetic head to the safe parking area. Once the sensor detects and the processor determines that the current gravity acceleration exceeds the safe value, the controller will stop the read and write work of the magnetic head through hardware control, quickly return to its position, and lock it in the exclusive magnetic head parking area. This series of actions will2complete within 0 milliseconds. The product will only resume operation after the detection device detects that the acceleration value has returned to the normal range.
The code of the Android multimedia framework is located in the following directory: external/opencore/. This directory is the root directory of the Android multimedia framework, and the subdirectories it contains are as follows:
* android: This is an upper-level library that implements a Player and Author for Android based on the PVPlayer and PVAuthor SDK
* baselibs: include fundamental libraries containing data structures and thread safety
* codecs_v2: This is a library with a lot of content, mainly including the implementation of codecs and an OpenMAX implementation
* engines: include the implementation of PVPlayer and PVAuthor engines
*extern_libs_v2: Includes khronos OpenMAX header files
*fileformats: Specific parsing (parser) classes for file formats
* nodes: Various node classes for encoding and file parsing
* oscl: Operating system compatibility library
* pvmi: Abstract interface for input/output control
* protocols: Mainly related to network-related RTSP, RTP, HTTP, etc. protocols
* pvcommon: Android.mk file of the pvcommon library file, no source file
*pvplayer: Android.mk file of the pvplayer library file, no source file
* pvauthor: Android.mk file of the pvauthor library file, no source file
* tools_v2: Compilation tools and some registrable modules
The following is part of the test code:
private SensorManager sensorMgr; Sensor sensor = sensorMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); //Save the coordinates of the last x, y, z float bx = 0; float by = 0; float bz = 0; long btime = 0;//This time sensorMgr = (SensorManager) getSystemService(SENSOR_SERVICE); SensorEventListener lsn = new SensorEventListener() { public void onSensorChanged(SensorEvent e) { float x = e.values[SensorManager.DATA_X]; float y = e.values[SensorManager.DATA_Y]; float z = e.values[SensorManager.DATA_Z]; //Calculate the numerical values of X, Y, Z below. You can calculate the swing speed based on these values. //Speed = Distance/Time //X-axis speed float speadX = (x - bx) / (System.currentTimeMillis() - btime); //y-axis speed float speadY = (y - by) / (System.currentTimeMillis() - btime); //z-axis speed float speadZ = (z - bz) / (System.currentTimeMillis() - btime); //This simple speed can be calculated, if you want to calculate acceleration, in kinematics, the acceleration a is related to speed, //The displacement is related: Vt=V0+at, S=V0*t+1/2at^2, S=(Vt^2-V0^2)/(2a), according to this information, we can also solve a bx = x; by = y; bz = z; btime = System.currentTimeMillis(); }; public void onAccuracyChanged(Sensor s, int accuracy) { }; }; // Register listener, the third parameter is the detection accuracy sensorMgr.registerListener(lsn, sensor, SensorManager.SENSOR_DELAY_GAME);
Readers who are interested in more about Android-related content can check the special topics on this site: 'Android Development入门与进阶教程', 'Android View View Skills Summary', 'Android Programming activity Operation Skills Summary', 'Android Operation SQLite Database Skills Summary', 'Android Operation json Format Data Skills Summary', 'Android Database Operation Skills Summary', 'Android File Operation Skills Summary', 'Android Programming Development SD Card Operation Methods Summary', 'Android Resource Operation Skills Summary', and 'Android Control Usage Summary'
I hope the description in this article will be helpful to everyone in Android program design.
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @ to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)