OnSwipeTouchListener.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package de.tudarmstadt.informatik.hostage.ui2.listeners;
  2. import android.view.GestureDetector;
  3. import android.view.GestureDetector.SimpleOnGestureListener;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.View.OnTouchListener;
  7. import de.tudarmstadt.informatik.hostage.ui.MainActivity;
  8. /**
  9. * @author Alexander Brakowski
  10. * @created 27.01.14 23:47
  11. */
  12. public class OnSwipeTouchListener implements OnTouchListener {
  13. private final GestureDetector gestureDetector = new GestureDetector(MainActivity.getContext(), new GestureListener());
  14. public boolean onTouch(final View view, final MotionEvent motionEvent) {
  15. return gestureDetector.onTouchEvent(motionEvent);
  16. }
  17. private final class GestureListener extends SimpleOnGestureListener {
  18. private static final int SWIPE_THRESHOLD = 250;
  19. private static final int SWIPE_VELOCITY_THRESHOLD = 70; //or 60
  20. @Override
  21. public boolean onDown(MotionEvent e) {
  22. return true;
  23. }
  24. @Override
  25. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  26. boolean result = false;
  27. try {
  28. float diffY = e2.getY() - e1.getY();
  29. float diffX = e2.getX() - e1.getX();
  30. if (Math.abs(diffX) > Math.abs(diffY)) {
  31. if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
  32. if (diffX > 0) {
  33. onSwipeRight();
  34. } else {
  35. onSwipeLeft();
  36. }
  37. }
  38. } else {
  39. if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
  40. if (diffY > 0) {
  41. onSwipeBottom();
  42. } else {
  43. onSwipeTop();
  44. }
  45. }
  46. }
  47. } catch (Exception exception) {
  48. exception.printStackTrace();
  49. }
  50. return result;
  51. }
  52. }
  53. public void onSwipeRight() {
  54. }
  55. public void onSwipeLeft() {
  56. }
  57. public void onSwipeTop() {
  58. }
  59. public void onSwipeBottom() {
  60. }
  61. }