手势GestureDetector

手势交互过程
OnTouchlistenner:手势交互接口根据手势识别结果调用相对应的方法
GestureDetector:手势识别器
MotionEvent:用封装手势、触摸板、轨迹球等动作事件记录属性X和Y
1、触屏的一刹那,触发MotionEvent事件
2、被OnTouchlistenner监听,在ontouch()中获得MotionEvent对象
3、GestureDetector转发MotionEvent对象至OnTouchlistenner----
----myGestureDetector.onTouchEvent(event)改变ontouch方法状态(true)
4、OnTouchlistenner获的该对象,根据该对象封装的信息做出合适的反馈
GestureDetector:手势识别器
按下、移动、抬起。

GestureDetector:手势识别器——原理:
1、当收到用户触摸消息时,将消息交给GestureDetector加工
2、通过设置监听器获取GestureDetector处理后的手势
GestureDetector提供两个监听器
1、Ongesturelistener:处理单击类消息
2、OnDoubleTaplistener:处理双击类消息
Ongesturelistener:接口有6个抽象方法:

1、单击:onDown(MotionEvent    e)
2、抬起:onSingleTapUp(MotionEvent    e);
3、短按:onshowpress(MotionEvent    e);
4、长按:onLongprss(MotionEvent    e);
5、滚动:onScroll(MotionEvent    e1,MotionEvent    e2,float velocityX,float velocityY);
6、滑动:onfling(MotionEvent    e1,MotionEvent    e2,float velocityX,float velocityY);
OnDoubleTaplistener:3个抽象方法:
双击:onDoubleTap(MotionEvent    e);
双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent    e)
单击确定:OnSingleTapConfirmed(MotionEvent    e);

GestureDetector进行手势识别
一、继承SimpleOnGesturelistener;只有抽象方法(onfling())
实例GestureDetector:new GestureDetector( new GestureDetector.SimpleOnGestureListener()
二、重载感兴趣的手势


package zxx.clipboard;


import android.app.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
	GestureDetector myGestureDetector;
	
	@SuppressWarnings("deprecation")//添加兼容
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ImageView image=(ImageView) findViewById(R.id.imag);
		image.setOnTouchListener(new OnTouchListener() {
		
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				myGestureDetector.onTouchEvent(arg1);
				return true;
			}
		});
		
		myGestureDetector=new GestureDetector( new GestureDetector.SimpleOnGestureListener(){
			@Override
			public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
				Log.i("*****", "jin滑動了");
				if (e1.getX()-e2.getX()>20) {
					Toast.makeText(getApplicationContext(), "從左往右滑", Toast.LENGTH_LONG).show();
				}
				if (e2.getX()-e1.getX()>20) {
					Toast.makeText(getApplicationContext(), "從右往左滑", Toast.LENGTH_LONG).show();
				}
				return super.onFling(e1, e2, velocityX, velocityY);
			}
		});
		
	}
	

	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值