Android uses accelerometer to achieve shake-and-shake function and optimization

Source: Internet
Author: User

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

    1. Sensormanager = (Sensormanager) context.getsystemservice (Context.sensor_service);


2. Get the acceleration sensor through the sensor manager

    1. Acceleratesensor = Getsensormanager (context). Getdefaultsensor (Sensor.type_accelerometer);


3. Register the Listener for the acceleration sensor

    1. 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.

  1. @Override
  2. public void Onsensorchanged (Sensorevent event) {
  3. int sensortype = Event.sensor.getType ();
  4. //values[0]:x shaft, values[1]:y shaft, VALUES[2]:Z shaft
  5. float[] values = event.values;
  6. float x = values[0];
  7. float y = values[1];
  8. float z = values[2];
  9. LOG.I (TAG, "x:" + x + "y:" + y + "z:" + z);
  10. LOG.I (TAG, "Math.Abs (x):" + math.abs (x) + "Math.Abs (y):" +math.abs (y) + "Math.Abs (z):" + Math.Abs (z));
  11. if (SensorType = = sensor.type_accelerometer) {
  12. int value = ; Shake the valve value, different mobile phones can reach the maximum value of different, such as a brand phone can only reach
  13. if (x >= value | | | x <=-value | | y >= value | | y <=-value | | z >= value | | z <=-value) {
  14. LOG.I (TAG, "shake detected");
  15. //Play the animation, update the interface, and do the corresponding business operations
  16. }
  17. }



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:

  1. Float[] values = event.values;
  2. float x = values[0];
  3. Float y = values[1];
  4. float z = values[2];
  5. LOG.I (TAG, "onsensorchanged:" + "x:" + x + ", y:" + y + ", Z:" + z ");
  6. if (x >= | | x <=-| | y >= | | y <=-| | z >= | | /c5>
  7. if (Allowshake ()) {//To determine if repeated shaking
  8. LOG.E (TAG, "Shake it, Shake It");
  9. New Allowshake (). Start ();
  10. } Else {
  11. LOG.E (TAG, "2s again allowed to shake");
  12. }
  13. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.