android手势识别 android使用gesturedetector手势识别示例分享
人气:0public class MyGestureLintener extends SimpleOnGestureListener {
private Context context;
public MyGestureLintener(Context context) {
super();
this.context = context;
}
// 单击,触摸屏按下时立刻触发
/*@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(context, "Down " + e.getAction(), Toast.LENGTH_SHORT)
.show();
return true;
}*/
// 双击,手指在触摸屏上迅速点击第二下时触发
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDoubleTap(e);
}
// 双击的按下跟抬起各触发一次
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDoubleTapEvent(e);
}
// 滑动,触摸屏按下后快速移动并抬起,会先触发滚动手势,跟着触发一个滑动手势
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return super.onFling(e1, e2, velocityX, velocityY);
}
// 长按,触摸屏按下后既不抬起也不移动,过一段时间后触发
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(context, "LONG " + e.getAction(), Toast.LENGTH_SHORT)
.show();
}
// 滚动,触摸屏按下后移动
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Toast.makeText(context, "onScroll " + e2.getAction(), Toast.LENGTH_SHORT)
.show();
return true;
}
// 短按,触摸屏按下后片刻后抬起,会触发这个手势,如果迅速抬起则不会
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(context, "Show " + e.getAction(), Toast.LENGTH_SHORT)
.show();
}
// 单击确认,即很快的按下并抬起,但并不连续点击第二下
/*@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(context, "onSingleTapConfirmed " + e.getAction(), Toast.LENGTH_SHORT)
.show();
return true;
}*/
// 抬起,手指离开触摸屏时触发(长按、滚动、滑动时,不会触发这个手势)
/*@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(context, "onSingleTapUp " + e.getAction(), Toast.LENGTH_SHORT)
.show();
return true;
}*/
public class MainActivity extends Activity {
private GestureDetector mGestureDetector;//手势对象
private MyGestureLintener myGestureLintener;//手势监听的接口对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myGestureLintener = new MyGestureLintener(this);
//手势对象的构造方法
mGestureDetector = new GestureDetector(this,
myGestureLintener);
}
/**GestureDetector类的onTouchEvent方法用来辨别不同的手势*/
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean b = false;
int i = event.getAction();
int j = MotionEvent.ACTION_MOVE;
System.out.println(i+"<----------------->"+j);
b = mGestureDetector.onTouchEvent(event);
if (b) {
Intent in = new Intent();
in.setClass(this, testActivity.class);
startActivity(in);
}
return b;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
加载全部内容