Android programming uses acceleration sensor to realize shake function and detailed explanation of optimization method

This paper gives an example of Android programming using acceleration sensor to realize the shake function and optimization method. Share with you for your reference, as follows:

At present, many applications have realized the shaking function. Here, we review the use of acceleration sensors by explaining the principle and implementation of this function:

1. First obtain an instance of the sensor manager

2. Obtain the acceleration sensor through the sensor manager

3. Register the listener of the acceleration sensor

Parameter Description:

Listener: instance of acceleration sensor listener: instance of acceleration sensor. The acceleration sensor rate is used to realize shaking. There are four constants to choose from

SENSOR_ DELAY_ Normal: match the change of screen direction, and the default sensor speed is sensor_ DELAY_ UI: matching user interface

If updating the UI, sensor is recommended_ DELAY_ GAME:

Match the game. It is recommended to use sensor for game development_ DELAY_ Fastest.: the fastest matching can be achieved

Choose according to the situation. Generally, choose the first one

Sensor listener: sensoreventlistener has two callback methods

Onsensorchanged (sensorevent) and onaccuracychanged (sensor, int accuracy)

The first is the corresponding method for the change of sensor value, and the second is the corresponding method for the change of reaction speed

Both methods will be called at the same time

Introduction to onsensorchanged (sensorevent)

nSensorChanged(SensorEvent event)

The values variable of the event instance is very important. According to different sensors, the meaning of the values in it is also different. Take the acceleration sensor as an example:

Values the type of this variable is a float [] array with up to 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 direction of the X axis is from left to right along the horizontal direction of the screen. If the phone is not square, the shorter side needs to be placed horizontally and the longer side needs to be placed vertically. The direction of the Y axis starts from the lower left corner of the screen and points to the top of the screen along the vertical direction of the screen. Place the mobile phone flat on the table, and the direction of Z axis is to point to the sky from the mobile phone.

We judge whether the mobile phone shakes. As long as the X, y and Z axes reach the set threshold, it means shaking.

I wrote a long time ago:

In order to enhance the robustness of the program, it is necessary to judge and prevent the shake event from being triggered multiple times at the same time:

For the method to judge whether it is repeated shaking, please refer to the previous article. The principle of Android's method to effectively prevent repeated clicking of buttons (see the article) is the same. Please modify the specific time threshold according to the needs. There will be no more description here

For more Android related content, readers who are interested can view the topics on this site: Android basic component usage summary, @l)u 419)u 2@, Android layout skills summary, Android resource operation skills summary, Android file operation skills summary, introduction and advanced tutorial of Android development, activity operation skills summary of Android programming, 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
分享
二维码
< <上一篇
下一篇>>