Created
March 13, 2015 18:53
-
-
Save cawka/e8bb78629bb128ac6a96 to your computer and use it in GitHub Desktop.
Helper to handle swipe gestures on Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -*- Mode:jde; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ | |
/** | |
* Copyright (c) 2015 Regents of the University of California | |
* | |
* This file is part of NFD (Named Data Networking Forwarding Daemon) Android. | |
* See AUTHORS.md for complete list of NFD Android authors and contributors. | |
* | |
* NFD Android is free software: you can redistribute it and/or modify it under the terms | |
* of the GNU General Public License as published by the Free Software Foundation, | |
* either version 3 of the License, or (at your option) any later version. | |
* | |
* NFD Android is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
* PURPOSE. See the GNU General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License along with | |
* NFD Android, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. | |
*/ | |
package net.named_data.nfd.util; | |
import android.content.Context; | |
import android.view.GestureDetector; | |
import android.view.GestureDetector.SimpleOnGestureListener; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.view.View.OnTouchListener; | |
/** | |
* @brief Helper to handle right to left swipe gestures | |
* Based on code by Mirek Rusin (http://stackoverflow.com/a/12938787/2150331) | |
*/ | |
public class OnSwipeTouchListener implements OnTouchListener { | |
public | |
OnSwipeTouchListener(Context ctx) | |
{ | |
m_gestureDetector = new GestureDetector(ctx, new GestureListener()); | |
} | |
public boolean | |
onTouch(final View view, final MotionEvent motionEvent) | |
{ | |
m_view = view; | |
return m_gestureDetector.onTouchEvent(motionEvent); | |
} | |
private final class GestureListener extends SimpleOnGestureListener { | |
private static final int SWIPE_THRESHOLD = 100; | |
private static final int SWIPE_VELOCITY_THRESHOLD = 100; | |
@Override | |
public boolean | |
onDown(MotionEvent e) | |
{ | |
return true; | |
} | |
@Override | |
public boolean | |
onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) | |
{ | |
boolean result = false; | |
try { | |
float diffY = e2.getY() - e1.getY(); | |
float diffX = e2.getX() - e1.getX(); | |
if (Math.abs(diffX) > Math.abs(diffY)) { | |
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { | |
if (diffX > 0) { | |
onSwipeRight(m_view); | |
} else { | |
onSwipeLeft(m_view); | |
} | |
} | |
result = true; | |
} | |
else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { | |
if (diffY > 0) { | |
onSwipeBottom(m_view); | |
} else { | |
onSwipeTop(m_view); | |
} | |
} | |
result = true; | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
return result; | |
} | |
} | |
/** | |
* @brief Override to process swipe-right event | |
*/ | |
public void onSwipeRight(View view) | |
{ | |
} | |
/** | |
* @brief Override to process swipe-left event | |
*/ | |
public void onSwipeLeft(View view) | |
{ | |
} | |
/** | |
* @brief Override to process swipe-top event | |
*/ | |
public void onSwipeTop(View view) | |
{ | |
} | |
/** | |
* @brief Override to process swipe-bottom event | |
*/ | |
public void onSwipeBottom(View view) | |
{ | |
} | |
///////////////////////////////////////////////////////////////////////////// | |
private final GestureDetector m_gestureDetector; | |
private View m_view; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment