Android OnTouch swipe up/down direction change -
i'm trying detect when swipe direction changed while user still swipes on screen.
i have (very basic) detecting swipe direction:
@override public boolean ontouch(view view, motionevent motionevent) { int action = motionevent.getactionmasked(); switch (action) { case motionevent.action_down: { log.d(tag, "ontouch: down _y = " + motionevent.getrawy()); mlasttouchy = mprevtouchy = motionevent.getrawy(); break; } case motionevent.action_move: { log.d(tag, "ontouch: move _y = " + motionevent.getrawy()); final float dy = motionevent.getrawy(); if (dy >= mlasttouchy) { /* move down */ } else { /* move */ } break; } case motionevent.action_cancel: case motionevent.action_outside: case motionevent.action_up: { log.d(tag, "ontouch: _y = " + motionevent.getrawy()); // snap page break; } } return true; }
what need detect when user changed direction of swipe. example, code above fails detect edge cases:
- start y = 100,
- move down until 150,
- move until 50,
- move down again until 90
this detected swipe because initial y higher last y
should want detect direction change of swipe there simple way:
private gesturedetector gesturedetector; @override protected void oncreate(bundle savedinstancestate) { findviewbyid(r.id.myview).setontouchlistener(this); gesturedetector = new gesturedetector(this, this); }
and implement ontouch , gesturelisteners this:
@override public boolean ontouch(view v, motionevent event) { return gesturedetector.ontouchevent(event); } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { if (distancey > 0){ // going } else { // going down } return true; } @override public boolean ondown(motionevent e) { return true; } //the rest of methods must implement...
Comments
Post a Comment