0

I am trying to implement fling. Problem is that I get it only when the X is on offset 0 (which means on the edge of the screen). When I try to have a fling in the middle of the screen nothing happens. This happens only on X. Y behaves OK. Here is the code:

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
        float velocityY) {
    try {

        if ((e1.getY() < CARD_POSITION) && (e2.getY() < CARD_POSITION)
                && (Math.abs(e1.getX() - e2.getX()) >SWIPE_MIN_DISTANCE )
                ) {
            Log.v("Fling:",Double.toString(e1.getX())); 
            return true;
        } else {


            return false;
        }
    } catch (Exception e) {
        return false;
    }

}

1 Answers1

0

I've encoutered a similar issue in the past. It ended up being a error in the math of how I was comparing the x loction to the min distance threshold I was using. Based on what you're describing in combination with the code posted, it sounds as though the x direction fling is not working beacuse the different between x1 and x2, after applying absolute value, is not coming out to what you think.

From 0 offset, swiping the whole screen, you're able to register a fing because, with that much distance, the different between the 2 values is enough to trigger your threshold. But from the middle of the screen, say where x1 = 400 and x2 = whever the move event is registered, you're not triggering that threshold.

What worked for me was removing absolute value

public boolean onFling(MovtionEvent e1, MotionEvent e2, float velocityX, float velocityY){

if(e2.getX() - e1.getX() > MIN_SWIPE_DISTANCE && Math.abs(velocityX)){
    Log.d("Swipe", "Left to right")
    return true;
} else if (e2.getX() - e1.getX() > MIN_SWIPE_DISTANCE && Math.abs(velocityX)){
    Log.d("Swipe", "Right to Left")
    return true;
}

My reasoning was based on the GestureDetector docs which describe what e1 and e2 represent:

e1 The first down motion event that started the scrolling.
e2 The move motion event that triggered the current onFling.

The error turned out to be an assumption in where th e1 intial ACTION_DOWN occurred relative to the ACTION_MOVE from e2

Rarw
  • 7,645
  • 3
  • 28
  • 46