I'm currently working on an Android application which allows the user to play a violin as they move their finger left and right across the screen. I wish to make the music stop playing when they stop moving their finger and the violin's bow stops moving.
I'm using a typical onTouchEvent to capture the movement gesture, and now I'm wondering the best way to detect the user is user is still touching the screen, but is not moving their finger.
This is my code at the moment with an extremely spaghetti function to detect the amount of movement, which runs into the following two problems:
1: As the user moves from left to right, it will register it as no movement for a split second and stop the sound.
2: It's impossible to hold your finger perfectly still, I need to allow for small amount of movements.
Can anyone suggest the correct way of detecting the lack of movement than below?
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
audio.start();
bowImage.setY(430);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
float posX = event.getX();
if ( detectValidMovement(posX) ) {
bowImage.setX(posX);
audio.start();
} else {
audio.pause();
}
} else if (event.getAction() == MotionEvent.ACTION_UP) {
audio.pause();
}
return true;
}
private boolean detectValidMovement(float posX) {
int newX = Math.round(posX);
if (oldX > 0) {
int distanceMoved = newX - oldX;
if (oldX != newX) {
oldX = newX;
return true;
} else {
oldX = newX;
return false;
}
} else {
oldX = newX;
}
return false;
}