Tuesday, April 1, 2014

UI for Android: Touchscreen




I have createda gesture listener class to handle these.

You have to manually set what constitutes a "swipe"-- how far the user has to move their finger (both min and max). You have to set the speed at which they move their finger, so as not to conflate with a "scroll":
  private int swipe_Min_Distance = 100;
    private int swipe_Max_Distance = 800;
    private int swipe_Min_Velocity = 40;
Then you catch the probable swipe as a "fling." You make sure it's a swipe and determine its direction--manually setting the leeway.
@Override
    public boolean onFling(
        MotionEvent e1,
        MotionEvent e2,
        float velocityX,
        float velocityY)
    {
        final float xDistance = Math.abs(e1.getX() - e2.getX());
        final float yDistance = Math.abs(e1.getY() - e2.getY());

        if(xDistance > this.swipe_Max_Distance || yDistance > this.swipe_Max_Distance)
         return false;

        velocityX = Math.abs(velocityX);
        velocityY = Math.abs(velocityY);
              boolean result = false;

        if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance){
         if(e1.getX() > e2.getX()) // right to left
          this.onSwipe(Globals.SWIPE_LEFT);
         else
          this.onSwipe(Globals.SWIPE_RIGHT);
         result = true;
        }
        else if(velocityY > this.swipe_Min_Velocity && yDistance > this.swipe_Min_Distance){
         if(e1.getY() > e2.getY()) // bottom to up
          this.onSwipe(Globals.SWIPE_UP);
         else
          this.onSwipe(Globals.SWIPE_DOWN);
         result = true;
        }
         return result;
    }
Have to differentiate between single taps, long presses and double taps:

 @Override
    public boolean onSingleTapConfirmed(MotionEvent e)
    {        float scaledX, scaledY;
        gs.touchCell(scaleX(e.getX()), scaleY(e.getY()), false);
        return true;
    }

 @Override
    public boolean onDoubleTapEvent(MotionEvent e)
    {
        gs.startListening();
        return true;
    }

No comments:

Post a Comment