关于手势,大神的博客:http://blog.youkuaiyun.com/harvic880925/article/details/39520901
用于处理一些复杂的手势活动。
//GestureDetector 手势
GestureDetector 这个类对外提供了两个接口和一个外部类
接口:OnGestureListener OnDoubleTapListener(双击的监听)
外部类:SimpleOnGestureListener
这个外部类,其实是两个接口中所有函数的集成,它包含了这两个接口里所有必须要实现的函数而且都已经重写,但所有方法体都是空的;不同点在于:该类是static class,程序员可以在外部继承这个类,重写里面的手势处理方法。
private class gesturelistener implements GestureDetector.OnGestureListener{
//手指轻轻的触摸屏幕的一瞬间,由1个ACTION_DOWN触发
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
//手指轻轻触摸屏幕,尚未松开或拖动 注意和onDown()的区别,没有松开或拖动的状态
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
//单击行为
/*
触发顺序:
点击一下非常快的(不滑动)Touchup:
onDown()->onSingleTapUp()->onSingleTapConfirmed()
点击一下稍微慢点的(不滑动)Touchup:
onDown()->onShowPress()->onSingleTapUp()->onSingleTapConfirmed()
*/
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
//手指按下屏幕并拖动
/*
滑屏:
onDown()->onScroll()->onScroll()->...->onFling()
拖动:
onDown()->onScroll()->onScroll()->...->onFling()
*/
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}
//用户长久地按着屏幕不放,即长按
//触发的顺序是onDown()->onShowPress()->onLongPress()
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
//用户按下触摸屏,快速滑动后松开
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
}
//使用GestureDetector
/*
分三步走:
1、 创建OnGestureListener监听函数
GestureDetector.OnGestureListener listener=new GestureDetector.OnGestureListener(){
//实现抽象方法
};
2、创建GestureDetector实例mGestureDetector
构造函数有三个:
GestureDetector gestureDetector=new GestureDetector(GestureDetector.OnGestureListener listener);
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.OnGestureListener listener);
GestureDetector gestureDetector=new GestureDetector(Context context,GestureDetector.SimpleOnGestureListener listener);
3、onTouch(View v,MotionEvent event)中拦截:
public boolean onTouch(View v,MotionEvent event){
return mGestureDetector.onTouchEvent(event);
}
4、绑定控件
TextView tv=(TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
*/
//最简单的代码
public class MainActivity extends Activity implements OnTouchListener{
private GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener
TextView tv = (TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
}
/*
在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector 来分析是否有合适的callback函数来处理用户的手势
*/
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
private class gestureListener implements GestureDetector.OnGestureListener{
//实现抽象方法
};
}
//GestureDetector.OnDoubleTapListener--接口
待续~