0

Is there any way to detect to which direction the phone was shaked? For example: I want something be shot from the right border of the screen, so I need to look if a phone was shaked to the left (coordinates from the beginning of first movement). And nearly the same for the left border. Hope you understand.

Thanks a lot!

Graykos
  • 1,281
  • 3
  • 20
  • 31
  • Just check the accelerometer reading while you do the action, and you should understand how to identify the gesture. Probably you can just check the acceleration in the particular coordinate you need. – codetiger Jul 26 '12 at 12:29

2 Answers2

1

You can detect the change in a specific direction by implementing SensorEventListener and checking:

public void onSensorChanged(SensorEvent se) {
    // Get sensor data.
    float x = se.values[SensorManager.DATA_X];
    float y = se.values[SensorManager.DATA_Y];
    float z = se.values[SensorManager.DATA_Z];
}

Then simply compare a few consecutive values of one of the variables to determine the direction. I think that X is the one you're interested in.

TofferJ
  • 4,678
  • 1
  • 37
  • 49
  • But this would also work when the phone simply rotates. And I need to handle quiet rough gesture, like it was shhhaked in a direction. I thought about memorizing the acceleration before direction change and compare it to one after it. And if it is bigger and also its bigger then some treshold (to avoid light movements) than it means that the phone was shaked in the direction of before-direction-changed direction. – Graykos Jul 26 '12 at 12:47
  • I cant understand if there are any Sensor events which have acceleration data in them – Graykos Jul 26 '12 at 12:48
  • You could check say X like you describe (with a treashold value), but at the same time make sure that the other two stay close to their initial values. This would tell you when a one-dimensional "shake" has occurred. (and not give you false alarm when the phone is rotating etc.) Isn't that what you want? Or do you explicitly need the acceleration? You could ofcourse use the consecutive X values to calculate the acceleration. I haven't seen an easy way in the Sensor library to simply get the acceleration as you suggest. :/ – TofferJ Jul 26 '12 at 12:52
1

Assuming that you don't need to measure the force of shaking for shooting-power purpose following link will surely help with your problem. A simple accelerometer tutorial

monish_sc
  • 146
  • 6