SimpleOnGestureListener//简单手势监听

本文详细介绍了Android中SimpleOnGestureListener类的使用方法及其主要方法onFling的具体实现。通过示例代码展示了如何创建手势监听器并处理不同类型的触摸事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SimpleOnGestureListener//简单手势监听

方法:
(1) public boolean  onFling   (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
Notified of a fling event when it occurs with the initial on down MotionEvent and the matching up MotionEvent. The calculated velocity is supplied along the x and y axis in pixels per second.
//通知一个抛(扔,掷)事件,当它随着一个最初的向下的运动事件和匹配的运动事件发生时。计算的速率是通过x,y轴上每秒的像素值确定的。
Parameters//参数
e1:The first down motion event that started the fling.//开始这个"抛"的第一次向下的运动事件.
e2:The move motion event that triggered the current onFling.//引起当前的"抛"的运动事件。
velocityX:The velocity of this fling measured in pixels per second along the x axis.//以沿着x轴某秒多少像素数测量的"抛"的速率.
velocityY:The velocity of this fling measured in pixels per second along the y axis.// 以沿着y轴某秒多少像素数测量的"抛"的速率.
Returns
  • true if the event is consumed, else false//如果事件被消耗,返回true,否则返回false.

例子:
(1)写一个类继承SimpleOngestureListener
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;


public class DefaultListener extends SimpleOnGestureListener {
	/**
	 * 两次轻敲时调用
	 */
	@Override
	public boolean onDoubleTap(MotionEvent e) {
		// TODO Auto-generated method stub


		int action = e.getAction();
		System.out.println("两次轻敲,onDoubleTap," + action);
		return super.onDoubleTap(e);
	}


	@Override
	// 按下时调用
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("按下,onDown==" + e.getAction());// onDown=0
		return super.onDown(e);
	}


	// 滑动一段距离,up时触发
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		System.out.println("抛," + "e1===" + e1.getAction() + ",e2==="
				+ e2.getAction());// 0
		System.out.println("velocityX===" + velocityX + ",velocityY==="
				+ velocityY);// 1
		return super.onFling(e1, e2, velocityX, velocityY);
	}


	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("两次轻敲事件,onDoubleTapEven==" + e.getAction());
		return super.onDoubleTapEvent(e);
	}


	// 长按后触发(Touch down之后一定时间(500ms))
	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("长按,onLongPress==" + e.getAction());
		super.onLongPress(e);
	}


	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		System.out.println("滑动时调用," + "e1===" + e1.getAction() + ",e2==="
				+ e2.getAction());
		return super.onScroll(e1, e2, distanceX, distanceY);
	}


	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("短按,onShowPress==" + e.getAction());
		super.onShowPress(e);
	}


	@Override
	// 按下时调用
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("onSingleTapConfirmed,单击确认==" + e.getAction());// 0
		return super.onSingleTapConfirmed(e);
	}


	@Override
	// 按下时调用
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		System.out.println("onSingleTapUp==" + e.getAction());// 1
		return super.onSingleTapUp(e);
	}


}
(2)在一个Activity里面重写onTouchEvent(),并且将上面的那个类的对象作为参数传递给GestureDetector。如下所示:
import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class MainActivity extends Activity {

	private GestureDetector gestureDetector;
	private DefaultListener defaultListener;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		defaultListener = new DefaultListener();
		gestureDetector = new GestureDetector(getApplicationContext(),
				defaultListener);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {

		return gestureDetector.onTouchEvent(event);
	}

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值