ahsan
ahsan

Reputation: 677

problem with swipe (fling) gesture recognition on ImageView in Android

I am trying to work a simple swipe/fling gesture recognizer on an ImageView in my app. However, I dont see anything. Can any one kindly have a look at the code and let me know what am I missing here ? Thanks.

Note1: from my debugging, I can see that execution does go inside the GestureListener. However, it gets a return false from there.

code:

public class ViewSuccess extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
ImageView movieFrame;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);



    imageLoader=new ImageLoader(getApplicationContext());




    movieFrame = (ImageView) findViewById(R.id.widget32);
    movieFrame.setImageResource(R.drawable.sample);





 // Gesture detection
    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

    movieFrame.setOnTouchListener(gestureListener);


class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

        System.out.println("in gesture recognizer !!!");

        try {
            if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                return false;
            // right to left swipe
            if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(ViewSuccess.this, "Left Swipe", Toast.LENGTH_SHORT).show();
            }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                Toast.makeText(ViewSuccess.this, "Right Swipe", Toast.LENGTH_SHORT).show();
            }
        } catch (Exception e) {
            // nothing
        }
        return false;
    }
}
}

Upvotes: 1

Views: 2165

Answers (1)

ahsan
ahsan

Reputation: 677

got it working.... we need to add the following method to the class SimpleOnGestureListener

 @Override
    public boolean onDown(MotionEvent e)
    {
        return true;
    }

Upvotes: 2

Related Questions