Android 触摸手势 OnGestureListener

OnGestureListener 可以处理一些简单的触摸手势,比如滑动,长按等,

OnGestureListener不能直接设置侦听的,可以设置OnTouchListener,让其指向OnGestureListener

public class GestureView extends View implements OnTouchListener, OnGestureListener
{
 GestureDetector mGestureDetector;

public GestureView(Context context)
 {
  super(context);

this.setOnTouchListener(this);
  mGestureDetector = new GestureDetector(this);
 }


 @Override
 public boolean onDown(MotionEvent e)
 {
  Log.i("@fred", "触摸手势:onDown");
  return true;
 }

@Override
 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
 {
  if (e2.getX() - e1.getX() > 50)
  {
   Log.i("@fred", "触摸手势:onFling");
  }
  return true;
 }

@Override
 public void onLongPress(MotionEvent e)
 {
  Log.i("@fred", "触摸手势:onLongPress");

}

@Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
 {
  Log.i("@fred", "触摸手势:onScroll");
  return true;
 }

@Override
 public void onShowPress(MotionEvent e)
 {
  Log.i("@fred", "触摸手势:onShowPress");
 }

@Override
 public boolean onSingleTapUp(MotionEvent e)
 {
  Log.i("@fred", "触摸手势:onSingleTapUp");
  return true;
 }


 @Override
 public boolean onTouch(View v, MotionEvent event)
 {
  // 将MotionEvent事件传到GestureDetector
  return  mGestureDetector.onTouchEvent(event);
 }

}

比如点击一下屏幕:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/506adcfd1cbfe42fe8fe29707567c020.html