Gesture example
in
Android Processing
•
1 year ago
Hi,
I needed to have a simple way to see if a gesture of swipe up, down, right or left had been carried out so I created this little class to help me out. I know that Android has the built in gesture recognition but this just seemed so much easier. You can easily attach a function to be called when a gesture has been detected and the minimum travel distance and diagonal offset can all be configured easily.
Example code
- /*
- Example code to show one way to work with gestures at Android devices.
- Swipe left, right, up and down to call different functions that changes the background color.
- Swipe settings such as how much directional swipe is allowed and minimum swipe length can be set easily in the constructor of the gesture class.
- Tested on
- osx
- Processing 2.0a4
- java version "1.6.0_29"
- Android SDK tools 16
- Samsung Galaxy Tab 10.1 Honeycomb 3.1
- made by
- david sjunnesson
- david@tellart.com
- Tellart.com
- 2012
- */
- Gestures g; // create a gesture object
- color backgroundColor;
- void setup() {
- backgroundColor=color(0);
- g=new Gestures(100,50,this); // iniate the gesture object first value is minimum swipe length in pixel and second is the diagonal offset allowed
- g.setSwipeUp("swipeUp"); // attach the function called swipeUp to the gesture of swiping upwards
- g.setSwipeDown("swipeDown"); // attach the function called swipeDown to the gesture of swiping downwards
- g.setSwipeLeft("swipeLeft"); // attach the function called swipeLeft to the gesture of swiping left
- g.setSwipeRight("swipeRight"); // attach the function called swipeRight to the gesture of swiping right
- }
- void draw() {
- background(backgroundColor); // draw the background with the color, this changes accordingly to what swipe is being carried out
- }
- // android touch event.
- public boolean surfaceTouchEvent(MotionEvent event) {
- // check what that was triggered
- switch(event.getAction()) {
- case MotionEvent.ACTION_DOWN: // ACTION_DOWN means we put our finger down on the screen
- g.setStartPos(new PVector(event.getX(), event.getY())); // set our start position
- break;
- case MotionEvent.ACTION_UP: // ACTION_UP means we pulled our finger away from the screen
- g.setEndPos(new PVector(event.getX(), event.getY())); // set our end position of the gesture and calculate if it was a valid one
- break;
- }
- return super.surfaceTouchEvent(event);
- }
- // function that is called when we are swiping upwards
- void swipeUp() {
- println("a swipe up");
- backgroundColor=color(100);
- }
- void swipeDown() {
- println("a swipe down");
- backgroundColor=color(150);
- }
- void swipeLeft() {
- println("a swipe left");
- backgroundColor=color(200);
- }
- void swipeRight() {
- println("a swipe right");
- backgroundColor=color(250);
- }
Gesture class
- import java.lang.reflect.Method;
- class Gestures {
- int maxOffset, minLength;
- String functionName;
- PVector startPos, endPos;
- PApplet pApp;
- Method[] m;
- Gestures(int minimum,int offSet,PApplet theApplet) {
- m=new Method[4];
- pApp = theApplet;
- maxOffset=offSet; //number pixels you are allowed to travel off the axis and still being counted as a swipe
- minLength=minimum; // number of pixels you need to move your finger to count as a swipe
- }
- // where did our motion start
- void setStartPos(PVector pos) {
- startPos=pos;
- }
- // where did it end and also call to check if it was a valid swipe
- void setEndPos(PVector pos) {
- endPos=pos;
- checkSwipe();
- endPos=new PVector();
- startPos=new PVector();
- }
- // check if it is a valid swipe that has been performed and if so perform the attached function
- void checkSwipe() {
- if (abs(startPos.x-endPos.x)>minLength&&abs(startPos.y-endPos.y)<maxOffset) {
- if (startPos.x<endPos.x) {
- performAction(2); // a swipe right
- }
- else {
- performAction(0); // a swipe left
- }
- }
- else {
- if (abs(startPos.y-endPos.y)>minLength&&abs(startPos.x-endPos.x)<maxOffset) {
- if (startPos.y<endPos.y) {
- performAction(3); // a swipe downwards
- }
- else {
- performAction(1); // a swipe upwards
- }
- }
- }
- }
- // call the function that we have defined with setAction
- void performAction(int direction) {
- if (m[direction] == null)
- return;
- try {
- m[direction].invoke(pApp);
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- }
- // define a function that should get called when the different swipes is done
- void setAction(int direction, String method) {
- if (method != null && !method.equals("")) {
- try {
- m[direction] = pApp.getClass().getMethod(method);
- }
- catch (SecurityException e) {
- e.printStackTrace();
- }
- catch (NoSuchMethodException e) {
- e.printStackTrace();
- }
- }
- }
- // attach a function to a left swipe
- void setSwipeLeft(String _funcName) {
- setAction(0, _funcName);
- }
- void setSwipeUp(String _funcName) {
- setAction(1, _funcName);
- }
- void setSwipeRight(String _funcName) {
- setAction(2, _funcName);
- }
- void setSwipeDown(String _funcName) {
- setAction(3, _funcName);
- }
- }
1