OnGestureListener 可以处理一些简单的触摸手势,比如滑动,长按等,
OnGestureListener不能直接设置侦听的,可以设置OnTouchListener,让其指向OnGestureListener
[java]
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);
}
}
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);
}
}
比如点击一下屏幕: