Android 的触摸事件详解及示例代码

  publicclassGestureActivityextendsActivityimplementsOnTouchListener,

  OnGestureListener {

  GestureDetector detector;

  publicGestureActivity(){

  detector = new GestureDetector(this);

  }

  publicvoidonCreate(BundlesavedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  TextView tv = (TextView) findViewById(R.id.TextView001);

  //设置tv的监听器

  tv.setOnTouchListener(this);

  tv.setFocusable(true);

  //必须,view才能够处理不同于Tap(轻触)的hold

  tv.setClickable(true);

  tv.setLongClickable(true);

  detector.setIsLongpressEnabled(true);

  }

  publicbooleanonTouch(View v,MotionEvent event) {

  returndetector.onTouchEvent(event);

  }

  // 用户轻触触摸屏,由1个MotionEventACTION_DOWN触发

  publicbooleanonDown(MotionEventarg0) {

  Log.i("MyGesture","onDown");

  Toast.makeText(this, "onDown",Toast.LENGTH_SHORT).show();

  returntrue;

  }

  publicvoidonShowPress(MotionEvent e) {

  Log.i("MyGesture","onShowPress");

  Toast.makeText(this, "onShowPress",Toast.LENGTH_SHORT).show();

  }

  // 用户(轻触触摸屏后)松开,由一个1个MotionEventACTION_UP触发

  publicbooleanonSingleTapUp(MotionEvent e) {

  Log.i("MyGesture","onSingleTapUp");

  Toast.makeText(this, "onSingleTapUp",Toast.LENGTH_SHORT).show();

  returntrue;

  }

  // 用户按下触摸屏、快速移动后松开,由1个MotionEventACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发

  publicbooleanonFling(MotionEvente1, MotionEvent e2, float velocityX, float velocityY) {

  Log.i("MyGesture","onFling");

  // 参数解释:

  // e1:第1个ACTION_DOWNMotionEvent

  // e2:最后一个ACTION_MOVEMotionEvent

  // velocityX:X轴上的移动速度,像素/秒

  // velocityY:Y轴上的移动速度,像素/秒

  // 触发条件 :

  // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒

  finalintFLING_MIN_DISTANCE = 100,FLING_MIN_VELOCITY = 200;

  if (e1.getX() - e2.getX() >FLING_MIN_DISTANCE &&Math.abs(velocityX) > FLING_MIN_VELOCITY){

  // Flingleft

  Log.i("MyGesture","Fling left");

  Toast.makeText(this, "FlingLeft",Toast.LENGTH_SHORT).show();

  } elseif (e2.getX() - e1.getX() >FLING_MIN_DISTANCE &&Math.abs(velocityX) > FLING_MIN_VELOCITY){

  // Flingright

  Log.i("MyGesture","Fling right");

  Toast.makeText(this, "FlingRight",Toast.LENGTH_SHORT).show();

  } elseif(e2.getY()-e1.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {

  // Flingdown

  Log.i("MyGesture","Fling down");

  Toast.makeText(this, "Flingdown",Toast.LENGTH_SHORT).show();

  } elseif(e1.getY()-e2.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {

  // Fling up

  Log.i("MyGesture","Fling up");

  Toast.makeText(this, "Flingup",Toast.LENGTH_SHORT).show();

  }

  returnfalse;

  }

  // 用户按下触摸屏,并拖动,由1个MotionEventACTION_DOWN, 多个ACTION_MOVE触发

  publicbooleanonScroll(MotionEvente1, MotionEvent e2, float distanceX, float distanceY) {

  Log.i("MyGesture","onScroll");

  Toast.makeText(this, "onScroll",Toast.LENGTH_LONG).show();

  returntrue;

  }

  // 用户长按触摸屏,由多个MotionEventACTION_DOWN触发

  publicvoidonLongPress(MotionEvent e) {

  Log.i("MyGesture","onLongPress");

  Toast.makeText(this, "onLongPress",Toast.LENGTH_LONG).show();

  }

  }