Implementation and Optimization of android "shake swing"

Source: Internet
Author: User

Recently, I was developing a Player Based on the android operating system. According to the same design concept as I did, "fewer and bigger buttons", after the second UI change, the buttons on the main interface are reduced to three: help buttons, play/pause, and volume control.

It is expected that the three buttons will be reduced again to two or even one. This is my "uidesign idea" and I think it is quite nice.

Delete is deleted. The key is how to implement the delete button function ......

This step of opening a file is implemented by using the "shake" Action. Swing the phone and a previously compiled activity that opens the file will be started.

In the SDK, google provides such an interface that we can use

Import android. hardware. Sensor;
Import android. hardware. SensorEvent;
Import android. hardware. SensorEventListener;
Import android. hardware. SensorListener;
Import android. hardware. SensorManager;

You can see the names of these packages in general, which is related to sensor.

Implement the SensorListener interface in my related activities

Public class HelloBear extends Activity implements SensorListener

Here I got a warning that google does not recommend you to use this interface. We recommend that you use another one that can implement the same functions ...... Amount ~ Forget it, use it ...... Haha

Then, register the region in oncreate

SensorManager sensorMgr = (SensorManager) getSystemService (SENSOR_SERVICE );
SensorMgr. registerListener (SensorListenerthis, SensorManager. SENSOR_ACCELEROMETER, SensorManager. SENSOR_DELAY_GAME );

According to the interface name, since it is a listener, you need to judge the action ......

When I was writing this section, I learned from the Section published on javaeye. Sorry, I forgot my author name ...... However, the general implementation process is as follows:

This interface is not used to determine the shake action, but only provides a three-dimensional vector of x y z. The idea is to measure the three-dimensional vector at intervals, then find a change speed ~ (It is called swing when the speed changes in all three directions.) When the speed reaches a critical value, the action is triggered. My action is to send intent to another activity.

In fact, I think there should be no major problems. After implementation, I discovered a very serious mistake or even a mistake.

We know that when a user shakes a mobile phone, the maximum speed you set is the critical value. Generally, after reaching the critical value, the speed will increase. If you do not control it, this action may be executed consecutively.

Without any control, my test result is that my intent will be executed three to five times on average ......

How can this problem be solved? Maybe my solution is not universal, because I have not completely solved the problem in an activity, but it can be used for reference ~ Because a lot of our intention is to switch between activities.

Add a boolean flag to the listener for judgment. Normally, the value is true. After opening (action occurs), set it to flase. After the activity (action opens) is destroyed, set it to true. activityForResult is required here.

For example, intent. setClass (HelloBear. this, ChooseBear. class );
HelloBear. this. startActivityForResult (intent, 0 );

Set the flag to normal in onActivityResult.

Problem solved

Finally, paste it to implement the core code of shake ~ It is basically the same as that on javaeye, but only the above optimization is carried out (this algorithm is simple, intuitive, but very SB ~ I want to find more powerful algorithms)

Public void onSensorChanged (int sensor, float [] values)
{
If (sensor = SensorManager. SENSOR_ACCELEROMETER ){

Long curTime = System. currentTimeMillis ();
// Detection every 10 milliseconds

If (curTime-lastUpdate) & gt; 100 ){
Long diffTime = (curTime-lastUpdate );
LastUpdate = curTime;

X = values [SensorManager. DATA_X];
Y = values [SensorManager. DATA_Y];
Z = values [SensorManager. DATA_Z];

Float speed = Math. abs (x + y + z-last_x-last_y-last_z)/diffTime x 10000;

If (speed> SHAKE_THRESHOLD & shakable = true)
{

Intent intent = new Intent ();
Intent. setClass (HelloBear. this, ChooseBear. class );
HelloBear. this. startActivityForResult (intent, 0 );
Shakable = false;
CancelDelayHide ();

}

Last_x = x;
Last_y = y;
Last_z = z;
}

}
}

Related Article

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.