[功能]
何为手势识别? 比如:你在屏幕上从左至右划出的一个动作 这就是手势 能够识别这个的就是 手势识别
[思路]
1. android 有一个手势识别的类:OnGestureListener
2. 在 GestureDetector() 中使用上面的class 即可 系统就会把手势交由该类来处理
public class SampleGuest implements OnGestureListener {
Activity activity;
public SampleGuest(Activity a){
activity = a;
}
// called automatically, any screen action will Triggered it
public boolean onTouchEvent(MotionEvent me){
return gesture.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onDown]");
return true;
}
@Override
//e1, the begin of ACTION_DOWN MotionEvent
//e2, the end of ACTION_DOWN MotionEvent
// velocityX, the motion speed in X
// velocityY:the motion speed in y
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if ((e1.getX() - e2.getX() > VALUE_DISTANCE)
&& Math.abs(velocityX) > VALUE_SPEED) {
Log.d("TAG","[+++++++++++][onFling][Fling left]");
} else if ((e2.getX() - e1.getX() > VALUE_DISTANCE)
&& Math.abs(velocityX) > VALUE_SPEED) {
Log.d("TAG","[+++++++++++][onDown][Fling right]");
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onLongPress]");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onScroll]");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onShowPress]");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Log.d("TAG","[+++++++++++][onSingleTapUp]");
return true;
}
}
2. 如何使用
SampleGuest sg = new SampleGuest(this);
GestureDetector gesture = new GestureDetector(sg);