使用GestrueDetector进行手势识别

手势交互过程原理:

1、手触屏的一刹那触发MotionEvent事件。

2、被OnTouchListener监听,在onTouch中获得MotionEvent对象。

3、GestrueDetector转发MotionEvent对象至OnGestureListener。

4、OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈。

MotionEvent:

1、用于封装手势、触摸笔、轨迹球等动作事件。

2、内部封装用于记录横轴和纵轴坐标的属性X和Y。

GestrueDetector:

识别各种手势。

OnGestureListener:

1、手势交互的监听接口,其提供各种抽象方法。

2、根据GestrueDetector的手势识别结果调用相对应的方法。

GestrueDetector提供两个监视器:

1、OnGestureListener:处理单击类消息。

2、OnDoubleTaPListener:处理双击类消息。

OnGestureListener接口:

单击:onDown(MotionEvent e);

抬起:onSingleTapUp(MotionEvent e);

短按:onShowPress(MotionEvent e);

长按:onLongPress(MotionEvent e);

滚动:onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY);

滑动:onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY);

OnDoubleTaPListener接口:

双击:onDoubleTap(MotionEvent e);

双击按下和抬起个触发一次:onDoubleTapEvent(MotionEvent e);

单机确认:onSingleTapConfirmed(MotionEvent e);即很快地按下并抬起,但并不连续点击第二下。

package com.example.gesturedetectordemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
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 {
ImageView imageView;
GestureDetector myGestureDetector;
class myGestureListener extends SimpleOnGestureListener{
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		// TODO Auto-generated method stub
		if (e1.getX()-e2.getX()>50) {
			Toast.makeText(MainActivity.this,"从右先左滑动", Toast.LENGTH_SHORT).show();
		}else if(e2.getX()-e1.getX()>50){
			Toast.makeText(MainActivity.this,"从左先右滑动", Toast.LENGTH_SHORT).show();	
		}
		return super.onFling(e1, e2, velocityX, velocityY);
	}
}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=(ImageView) findViewById(R.id.img);
        myGestureDetector=new GestureDetector(new myGestureListener()); 
        imageView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				myGestureDetector.onTouchEvent(event);
				return true;
			}
		});
    }
}

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.gesturedetectordemo.MainActivity" >
 
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/clockbg" />

</RelativeLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值