When playing games, the general use of a variety of sensors, in order to achieve a richer UI interaction, such as the detection of mobile phone shaking.
How do you measure the shaking and shaking of your phone?
Some information was found and improved to encapsulate this function into a class (Shakedetector) for future use.
Http://blog.csdn.net/ZhengZhiRen/archive/2010/10/09/5930451.aspx
The shake detection is based on the acceleration sensor (sensor.type_accelerometer).
Due to the presence of gravity, the acceleration sensor also has speed when the phone is stationary on the desktop.
Therefore, it is not possible to judge the shaking by only adding speed.
So, judging the change in acceleration ...
The difference in acceleration is calculated at a shorter time interval, compared to a specified threshold value, and if the difference is greater than the threshold, it is assumed that the wobble occurred.
The Clingmarks method adds the acceleration difference in the X, y, and z directions simply, which I think is not very accurate.
Acceleration is a vector, the difference should be the difference in the direction of the square after the sum, and then the root. (Math forgot, remember wrong ...) )
Monitor the class that shakes your phone:
/**
* @author Renxinwei
*
* @date 2013-1-17 09:19:58
*/
public class Sensormanagerhelper implements Sensoreventlistener {
//Speed threshold, which occurs when the shaking speed reaches this value
private static final int speed_shreshold =;
Time interval for two detections
private static final int uptate_interval_time =;
Sensor Manager
Private Sensormanager Sensormanager;
Sensor
Private sensor sensor;
Gravity Sense listener
Private Onshakelistener Onshakelistener;
Contextual object Context
Private context context;
The gravity sensor coordinates
Private float lastx in the previous position of the phone;
private float lasty;
Private float Lastz;
//Last detection time
private long lastupdatetime;
Constructor
Public Sensormanagerhelper (context context) {
//Get Listener object
This.context = Context;
Start ();
}
Begin
public void Start () {
Get the sensor manager
Sensormanager = (Sensormanager) context
. Getsystemservice (Context.sensor_service);
if (Sensormanager! = null) {
Obtaining a gravity sensor
Sensor = Sensormanager.getdefaultsensor (Sensor.type_accelerometer);
}
Registered
if (sensor! = NULL) {
Sensormanager.registerlistener (this, sensor,
Sensormanager.sensor_delay_game);
}
}
Stop detection
public void Stop () {
Sensormanager.unregisterlistener (this);
}
Shaking the Listening interface
Public interface Onshakelistener {
public void Onshake ();
}
Setting up a gravity sensor listener
public void Setonshakelistener (Onshakelistener listener) {
Onshakelistener = listener;
}
/*
* (Non-javadoc)
* Android.hardware.sensoreventlistener#onaccuracychanged (Android.hardware
* . Sensor, int)
*/
@Override
public void onaccuracychanged (sensor sensor, int accuracy) {
TODO auto-generated Method Stub
}
/*
* gravity sensor sensing for change data
* android.hardware.sensoreventlistener#onsensorchanged (android.hardware
*). sensorevent)
*/
@Override
public void onsensorchanged (Sensorevent event) {
//TODO auto-generated method Stub
//Now detection time
Long currentupdatetime = System.currenttimemillis ();
Time interval for two detections
long timeinterval = currentupdatetime-lastupdatetime;
Determine if the detection interval is reached
if (TimeInterval < uptate_interval_time) return;
Now the time becomes last time
LastUpdateTime = currentupdatetime;
Obtains x, y, z coordinates
float x = event.values[0];
Float y = event.values[1];
Float z = event.values[2];
//Get change values for x, y, z
float deltax = x-lastx;
float DeltaY = y-lasty;
Float Deltaz = Z-lastz;
//To change the current coordinates to the last coordinate
Lastx = x;
Lasty = y;
Lastz = z;
Double speed = math.sqrt (DeltaX * deltax + deltay * deltay + Deltaz
* Deltaz)
/timeinterval * 10000;
To reach the speed threshold, issue a prompt
if (>= speed_shreshold)
Onshakelistener.onshake ();
}
}
Use in activity:
Sensormanagerhelper sensorhelper = new Sensormanagerhelper (this);
Sensorhelper.setonshakelistener (New Onshakelistener () {
@Override
public void Onshake () {
TODO auto-generated Method Stub
System.out.println ("Shake");
Toast.maketext (Mainactivity.this, "You're shaking Oh", toast.length_short). Show ();
}
});
(Android) mobile phone shake detection.