基础知识请看:
http://www.cnblogs.com/akira90/archive/2013/03/10/2952886.html
http://www.oschina.net/question/12_1515
http://blog.csdn.net/song_shi_chao/article/details/8224742
我的理解:
有两种情况:
1、触摸这个activity
2、触摸某一个View
一、先说activity,
a、implements OnGestureListener
b、GestureDetector mGestureDetector = new GestureDetector(this);
c、重写activity的onTouchEvent方法
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
d、就可以捕捉到onFling事件
二、单独View
a、implements OnGestureListener,OnTouchListener
b、 View view = findViewById(R.id.view);
view.setOnTouchListener(this);
view.setLongClickable(true);
GestureDetector mGestureDetector = new GestureDetector(this);
c、重写的是implements OnTouchListener的onTouch方法
@Override
public boolean onTouch(View view, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
d、就可以捕捉到onFling事件
三、实现onFling
private float FLING_MIN_DISTANCE = 100;
private float FLING_MIN_VELOCITY=10;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
System.out.println("onFling");
if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// Fling left
System.out.println("左");
Toast.makeText(this, "左", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
&& Math.abs(velocityX) > FLING_MIN_VELOCITY) {
// Fling right
System.out.println("右");
Toast.makeText(this, "右", Toast.LENGTH_SHORT).show();
}
return false;
}