Last active
September 11, 2017 11:28
-
-
Save MartinRGB/2c812c2b7845ceed6fbe69c58d4f5c42 to your computer and use it in GitHub Desktop.
Gesture Utils
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
collection head |
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
package com.martinrgb.multitask; | |
import android.content.Context; | |
import android.util.Log; | |
import android.view.GestureDetector; | |
import android.view.MotionEvent; | |
import android.view.View; | |
import android.widget.Toast; | |
/** | |
* Created by MartinRGB on 2017/9/11. | |
*/ | |
public class MultiGestureDetector implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener,View.OnTouchListener{ | |
private static final int SWIPE_DISTANCE_THRESHOLD = 200; | |
private static final int SWIPE_VELOCITY_THRESHOLD = 1500; | |
private static final int LOG_TYPE_I = 0; | |
private static final int LOG_TYPE_E = 1; | |
private static final String TAG = "MultiGestureDetector"; | |
private GestureDetector mGestureDetector; | |
//创建监听器借口 | |
public interface SimpleGestureListener | |
{ | |
void onDown(MotionEvent event); | |
void onLongPress(MotionEvent event); | |
void onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY); | |
void onSwipeLeft(float velocity); | |
void onSwipeRight(float velocity); | |
void onSwipeBottom(float velocity); | |
void onSwipeTop(float velocity); | |
void onUp(MotionEvent event); | |
void onDoubleTap(MotionEvent event); | |
} | |
//实例化一个监听器的数值为空 | |
private SimpleGestureListener mSimpleGestureListener = null; | |
//添加监听器注册方法 | |
public void addListener(SimpleGestureListener simpleGestureListener) | |
{ | |
mSimpleGestureListener = simpleGestureListener; | |
} | |
public MultiGestureDetector(SimpleGestureListener simpleGestureListener) { | |
mGestureDetector = new GestureDetector(this); | |
mSimpleGestureListener = simpleGestureListener; | |
} | |
public boolean onDown(MotionEvent event) { | |
printTag(TAG,true,LOG_TYPE_I,event); | |
//mSimpleGestureListener.onDown(event); | |
return true; | |
} | |
public void onShowPress(MotionEvent event) { | |
printTag(TAG,true,LOG_TYPE_I,event); | |
} | |
public void onLongPress(MotionEvent event) { | |
printTag(TAG,true,LOG_TYPE_I,event); | |
mSimpleGestureListener.onLongPress(event); | |
} | |
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
printTag(TAG,true,LOG_TYPE_E,null); | |
Log.e("SFACT","onScroll" + " Distance X is " + Float.toString(e2.getX() - e1.getX()) + " Distance Y is " + Float.toString(e2.getY() - e1.getY())); | |
mSimpleGestureListener.onScroll(e1,e2,e2.getX()-e1.getX(),e2.getY()-e1.getY()); | |
return true; | |
} | |
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_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { | |
if (diffX > 0) { | |
//onSwipeRight(); | |
mSimpleGestureListener.onSwipeRight(velocityX); | |
Log.e("SFACT","Swipe Right,Velocity is" + velocityX); | |
} else { | |
//onSwipeLeft(); | |
mSimpleGestureListener.onSwipeLeft(velocityX); | |
Log.e("SFACT","Swipe Left,Velocity is" + velocityX); | |
} | |
result = true; | |
} | |
} | |
else if (Math.abs(diffY) > SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { | |
if (diffY > 0) { | |
// onSwipeBottom(); | |
mSimpleGestureListener.onSwipeBottom(velocityY); | |
Log.e("SFACT","Swipe Bottom,Velocity is" + velocityY); | |
} else { | |
//onSwipeTop(); | |
mSimpleGestureListener.onSwipeTop(velocityY); | |
Log.e("SFACT","Swipe Top,Velocity is" + velocityY); | |
} | |
result = true; | |
} | |
} catch (Exception exception) { | |
exception.printStackTrace(); | |
} | |
return result; | |
// printTag(TAG,true,LOG_TYPE_E,null); | |
// return true; | |
} | |
public boolean onTouch(View v, MotionEvent event) { | |
Log.v("ADAM", "ontouch: " + event.getAction()); | |
if(event.getAction() == 0){ | |
mSimpleGestureListener.onDown(event); | |
Log.v("ADAM", "Down"); | |
} | |
else if (event.getAction() == 1) { | |
Log.v("ADAM", "Up"); | |
mSimpleGestureListener.onUp(event); | |
} | |
else if (event.getAction() == 2) { | |
Log.v("ADAM", "Scroll"); | |
} | |
else if (event.getAction() == MotionEvent.ACTION_CANCEL) { | |
Log.v("ADAM", "Cancel"); | |
mSimpleGestureListener.onUp(event); | |
} | |
boolean detectedUp = event.getAction() == MotionEvent.ACTION_UP; | |
if (!mGestureDetector.onTouchEvent(event) && detectedUp) { | |
Log.v("ADAM", "Cancel"); | |
mSimpleGestureListener.onUp(event); | |
return true; | |
} | |
return true; | |
// printTag(TAG,true,LOG_TYPE_I,event); | |
// return mGestureDetector.onTouchEvent(event); | |
} | |
public boolean onSingleTapUp(MotionEvent event) { | |
printTag(TAG,true,LOG_TYPE_I,event); | |
//mSimpleGestureListener.onUp(event); | |
return false; | |
} | |
@Override | |
public boolean onSingleTapConfirmed(MotionEvent event) { | |
printTag(TAG,true,LOG_TYPE_I,event); | |
return false; | |
} | |
@Override | |
public boolean onDoubleTap(MotionEvent event) { | |
printTag(TAG,true,LOG_TYPE_I,event); | |
return false; | |
} | |
@Override | |
public boolean onDoubleTapEvent(MotionEvent event) { | |
printTag(TAG,true,LOG_TYPE_I,event); | |
mSimpleGestureListener.onDoubleTap(event); | |
return false; | |
} | |
private void printTag(String tag,boolean show,int LOGTYPE,MotionEvent event){ | |
final String suffix; | |
if(event == null){ | |
suffix = ""; | |
} | |
else{ | |
int X = (int) event.getX(); | |
int Y = (int) event.getY(); | |
suffix = " - X: "+X+" Y: "+Y; | |
} | |
if(show){ | |
if(LOGTYPE == 0){ | |
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); | |
StackTraceElement e = stacktrace[3];//maybe this number needs to be corrected | |
String methodName = e.getMethodName(); | |
Log.i(tag,methodName + suffix); | |
} | |
else if(LOGTYPE == 1){ | |
StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace(); | |
StackTraceElement e = stacktrace[3];//maybe this number needs to be corrected | |
String methodName = e.getMethodName(); | |
Log.e(tag,methodName + suffix); | |
} | |
} | |
else{ | |
} | |
} | |
} |
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
... | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) { | |
View view = inflater.inflate(R.layout.fragment_container, container, false); | |
setSpringSystem(view); | |
setInteractionListener(view); | |
return view; | |
} | |
private void setGestureListener(){ | |
MultiGestureDetector mMultiGestureDetector = new MultiGestureDetector(new MultiGestureDetector.SimpleGestureListener() { | |
@Override | |
public void onDown(MotionEvent event) { | |
} | |
@Override | |
public void onLongPress(MotionEvent event) { | |
} | |
@Override | |
public void onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { | |
} | |
@Override | |
public void onSwipeLeft(float velocity) { | |
} | |
@Override | |
public void onSwipeRight(float velocity) { | |
} | |
@Override | |
public void onSwipeBottom(float velocity) { | |
} | |
@Override | |
public void onSwipeTop(float velocity) { | |
} | |
@Override | |
public void onUp(MotionEvent event) { | |
} | |
@Override | |
public void onDoubleTap(MotionEvent event) { | |
} | |
}); | |
view.findViewById(R.id.xxxx).setOnTouchListener(mMultiGestureDetector); | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment