4
votes

I'm kinda new to Android programming and I recently turned my attention to handling simple gestures. I know about the GestureDetector and the listener and I succesfully implemented simple gestures (onDown, onFling, onScroll) which I need. The problem is I need the onUp method which is not available in the SimpleOnGestureListener class. I have an activity and a custom View. The view doesn't really do anything, just changes the background color. I'm doing all the event handling in the activity.

What I tried is handling the ACTION_UP event in the view's onTouchListener's onTouch method, but this way onFLing won't work. I also read about a solution here: OnUp event on GestureDetector ,which says I should overwrite the onTouchEvent but it was unclear to me which onTouchEvent should I overwrite, where and how. I'm inserting my current code, maybe it helps. :)

public class Main extends Activity 
{
private GestureDetector gDetector;
public CustomView cView;
View.OnTouchListener gestureListener;

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;

public Vibrator vibrator;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    vibrator = (Vibrator)getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
    gDetector=new GestureDetector(this, new gListener());
    gDetector.setIsLongpressEnabled(true);
    gestureListener=new View.OnTouchListener(){

        public boolean onTouch(View v, MotionEvent event) 
        {
            if(event.getAction()==MotionEvent.ACTION_UP)
            {
                cView.setBackgroundColor(Color.RED);
                return true;
            }
            else
            return gDetector.onTouchEvent(event);
        }
    };
    cView=new CustomView(this);
    cView.setOnTouchListener(gestureListener);
    cView.setBackgroundColor(Color.BLACK);
    cView.setLongClickable(true);
    setContentView(cView);
}

class gListener extends GestureDetector.SimpleOnGestureListener
{

     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
     {
         try 
         {
             if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                 return false;
             if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 cView.setBackgroundColor(Color.WHITE);
             }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 cView.setBackgroundColor(Color.GRAY);
             }
         } catch (Exception e) {
             // nothing
         }
         return false;
     }

     @Override
     public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
     {
         vibrator.vibrate(30);
         Log.d("Event", "MOVE");
         return true;
     }

     @Override
     public boolean onDown(MotionEvent e)
     {
         cView.setBackgroundColor(Color.BLUE);
         Log.d("Event", "DOWN");
         return true;
     }

     @Override
     public boolean onSingleTapUp(MotionEvent e)
     {
         Log.d("Event", "UP"); 
         return true;
     }

}

}

1
would the onFling still work if you return false in onSingleTapUp()?David T.

1 Answers

4
votes

Try this method...

@Override
public boolean onTouchEvent(MotionEvent me) {

Log.v("Touch", "ontouch: " + me.getAction());

if(me.getAction() == 0){
    Log.v("Touch Down", "Down");
}
else if (me.getAction() == 1) {
    Log.v("Touch Up", "Up");
}
else if (me.getAction() == 2) {
    Log.v("touch Scroll", "Scroll");
}
boolean detectedUp = event.getAction() == MotionEvent.ACTION_UP;
if (!mGestureDetector.onTouchEvent(event) && detectedUp)
{
    return onUp(event);
}
return true;
}