2015/11/13
在activity中实例化 手势识别对象
为gestureDetector对象设置 手势监听
GestureDetector gestureDetector;
gestureDetector =new GestureDetector(this,new MyGestureDetector(MainActivity.this));
//这里我自己把MainActivity.this传进去了,为了里面Toast可以使用activity的context
在activty中重写onTouch方法
//手势识别
@Override
public boolean onTouchEvent(MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
自定义的手势监听类
package com.example.myGestureDetector;
import com.example.testsometing.MainActivity;
import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Toast;
public class MyGestureDetector extends SimpleOnGestureListener{
//获得activty的context~~~
Context context;
public MyGestureDetector(Context context) {
this.context=context;
}
//双击触发
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(context,"onDoubleTap", Toast.LENGTH_SHORT).show();
return super.onDoubleTap(e);
}
//单击触发,单击事件
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Toast.makeText(context,"onSimpleTap", Toast.LENGTH_SHORT).show();
return super.onSingleTapConfirmed(e);
}
// 双击的第二下 down和up都会触发,可用e.getAction()区分。
//e.getAction()=0说明是onDown,=1则是抬起动作
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Log.v("onDoubleTapEvent",e.getAction()+"");
return super.onDoubleTapEvent(e);
}
//点击触发
@Override
public boolean onDown(MotionEvent e) {
Log.v("onDown",e.getAction()+"");
return super.onDown(e);
}
//滑动, Touch了滑动一点距离后,up时触发(滑动的速度慢不会触发)
//velocityX X轴上的移动速度,像素/秒 ;velocityY Y轴上的移动速度,像素/秒.
//触发条件:X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.v("onFling","e1"+e1.getAction()+":e2"+e2.getAction()+":x"+velocityX+":y"+velocityY);
return super.onFling(e1, e2, velocityX, velocityY);
}
//长按事件
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(context,"onLongPress", Toast.LENGTH_SHORT).show();
super.onLongPress(e);
}
//滑动就会触发
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.v("onScroll","onScroll");
return super.onScroll(e1, e2, distanceX, distanceY);
}
//Touch了还没滑动时候触发(跟onDown有什么区别?)
//经过测试很难控制触发,有时候onDown后会触发,有时候onscroll触发,onshowpress也没触发- -
@Override
public void onShowPress(MotionEvent e) {
Log.v("onShowPress","onShowPress");
super.onShowPress(e);
}
//单击事件结束时触发,先执行onSingleTapUp再onSingleTapConfirmed
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.v("onSingleTapUp","onSingleTapUp");
return super.onSingleTapUp(e);
}
}
还有自定义view的使用方法。(没试过)
在View中设置手势有两点需要注意:
1:View必须设置longClickable为true,否则手势识别无法正确工作,只会返回Down, Show, Long三种手势。
2:必须在View的onTouchListener中调用手势识别,而不能像Activity一样重载onTouchEvent,否则同样手势识别无法正确工作。
package com.sun.gesturetest;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class GestureView extends View {
private GestureDetector mDetector;
public GestureView(Context context, AttributeSet set) {
super(context, set);
mDetector = new GestureDetector(context, new MyGestureListener());
setLongClickable(true);
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return mDetector.onTouchEvent(event);
}
});
}
}