Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: how to detect horizontal and vertical gesture lines

I have some objects which I want to connect with lines. The user should be able to do so with a simple line-gesture. I use a GestureOverlayView and read that article http://developer.android.com/resources/articles/gestures.html, which says the following

orientation: indicates the scroll orientation of the views underneath. In this case the list scrolls vertically, which means that any horizontal gestures (like action_delete) can immediately be recognized as a gesture. Gestures that start with a vertical stroke must contain at least one horizontal component to be recognized. In other words, a simple vertical line cannot be recognized as a gesture since it would conflict with the list's scrolling.

And that is my problem - I want to draw the lines horizontal and vertical

Now I have a OnGesturePerfomedListener, which does the normal gesture recognition and additionally a GestureOverlayView.OnGestureListener, in which I detect the lines. But now I want to draw a dashed line - also vertical and horizontal. It would be so much easier, if I could get the complete gesture as in the OnGesturePerformedListener, instead of every single stroke of the dashed line, as in the onGestureListener.

Any ideas how I can solve this easily ? Is there a method, which is called when the gesturing is done, even if it is not recognised ? I also tried to use the GestureDetector.OnGestureListener, which I now use to detect longPress, but it won't help with that problem.

like image 413
alchra Avatar asked Nov 04 '22 22:11

alchra


1 Answers

Found a similar solution in other threads which might be useful to you (and anyone else who may come across this issue). This will most likely need serious tinkering to be adapted for your purposes but I think it will get you close:

//Swipe direction detection constants
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;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);

    //Gesture detection
    this.gestureDetector = new GestureDetectorCompat(this,this);
    gestureDetector.setOnDoubleTapListener(this);
}

//On gesture...
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
    return true;
}

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

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

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

@Override
public void onShowPress(MotionEvent e) {

}

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

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return true;
}

@Override
public void onLongPress(MotionEvent e) {

}

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

    try {
        // right to left swipe
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //DO SOMETHING...
        }
        // left to right swipe
        else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //DO SOMETHING...
        }
        // top to bottom swipe
        if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            //DO SOMETHING...
        }
        // bottom to top swipe
        else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            //DO SOMETHING...
        }
    } catch (Exception e) {
        return false;
    }

    return true;

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}
like image 118
Randall Arms Avatar answered Nov 11 '22 10:11

Randall Arms