If reproduced, please declare the source: The Sands of Time: http://blog.csdn.net/t12x3456
At present, many applications have realized the shaking function, here by explaining the principle of the function and implementation of the accelerometer to review the use of:
1. First obtain an instance of the sensor manager
- Sensormanager = (Sensormanager) context.getsystemservice (Context.sensor_service);
2. Get the acceleration sensor through the sensor manager
- Acceleratesensor = Getsensormanager (context). Getdefaultsensor (Sensor.type_accelerometer);
3. Register the Listener for the acceleration sensor
- Sensormanager.registerlistener (listener, sensor, rate)
Parameter description:
Listener: accelerating sensor listener instances
sensor: Speed up the sensors example to achieve shake shake using the acceleratesensor
Rate: Sensor reaction speed, there are four constants total selection
sensor_delay_normal: Match screen orientation change, default sensor speed
sensor_delay_ui: Matching user interface
If the update UI recommends using Sensor_delay_game:
Match games, game development recommends using Sensor_delay_fastest.: Match the fastest you can reach
Depending on the situation, the general choice of the first type can be
Sensor listener:Sensoreventlistener has two callback methods
onsensorchanged (sensorevent event) and onaccuracychanged (sensor sensor, int accuracy)
The first is the corresponding method of sensor value change
The second is the corresponding method of reaction velocity change.
Two methods are called at the same time
onsensorchanged (sensorevent event) Introduction
Nsensorchanged (Sensorevent event)
The values of the instance of the event are very important, depending on the sensor, the value inside represents a different meaning, to speed up the sensor as an example:
Values the type of the variable is an array of float[], with a maximum of three elements:
float x = Values[0] represents the x-axis
Float y = values[1] represents the y-axis
float z = values[2] represents the y-axis
The x-axis direction is left-to-right along the horizontal direction of the screen. If the phone is not a square, the shorter edges need to be placed horizontally, and the longer edges need to be placed vertically. The y-axis is oriented from the lower-left corner of the screen to the top of the screen along the vertical direction of the screen. Place your phone flat on the table, and the z-axis is directed from the phone to the sky.
We determine whether the phone shakes, as long as the x, Y, Z axis, to achieve the set threshold value is a shake.
- @Override
- public void Onsensorchanged (Sensorevent event) {
- int sensortype = Event.sensor.getType ();
- //values[0]:x shaft, values[1]:y shaft, VALUES[2]:Z shaft
- float[] values = event.values;
- float x = values[0];
- float y = values[1];
- float z = values[2];
- LOG.I (TAG, "x:" + x + "y:" + y + "z:" + z);
- LOG.I (TAG, "Math.Abs (x):" + math.abs (x) + "Math.Abs (y):" +math.abs (y) + "Math.Abs (z):" + Math.Abs (z));
- if (SensorType = = sensor.type_accelerometer) {
- int value = ; Shake the valve value, different mobile phones can reach the maximum value of different, such as a brand phone can only reach
- if (x >= value | | | x <=-value | | y >= value | | y <=-value | | z >= value | | z <=-value) {
- LOG.I (TAG, "shake detected");
- //Play the animation, update the interface, and do the corresponding business operations
- }
- }
I wrote it a long time ago:
In order to enhance the robustness of the program, it is necessary to judge and prevent the shaking of an event to be triggered multiple times at the same time:
- Float[] values = event.values;
- float x = values[0];
- Float y = values[1];
- float z = values[2];
- LOG.I (TAG, "onsensorchanged:" + "x:" + x + ", y:" + y + ", Z:" + z ");
- if (x >= | | x <=-| | y >= | | y <=-| | z >= | | /c5>
- if (Allowshake ()) {//To determine if repeated shaking
- LOG.E (TAG, "Shake it, Shake It");
- New Allowshake (). Start ();
- } Else {
- LOG.E (TAG, "2s again allowed to shake");
- }
- }