您的位置:首页 > 移动开发 > Android开发

Android学习(十六) 通过GestureDetector进行手势识别

2015-04-08 18:11 429 查看
一、手势交互过程:

  1)触屏时,触发MotionEvent事件。

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

  3)GestureDetector转发MotionEvent对象至OnGestureListener。

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

二、需要用到的类和接口

  1、MotionEvent:

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

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

  

  2、GestureDetector:

  识别各种手势:按下,移动,弹起。

  3、OnGestureListener接口

  1)手势交互的监听接口,其提供多个抽象方法。

    a) onDown(MotionEvent e); //单击

    b) onSingleTapUp(MotionEvent e); //抬起

    c) onShowPress(MotionEvent e); //短按

    d) onLongPress(MotionEvent e); //长按

    e) onScoll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) //滚动

    f) onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY) //滑动

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

  4、OnDoubleTapListener接口:

    a) onDoubleTap(MotionEvent e); //双击

    b) onDoubleTapEvent(MotionEvent e); //双击按下和抬起各触发一次

    c) onSingleTapConfirmed(MotionEvent e); //单击确认,很快的按下并弹起,但不点击第二下。

  5、SimpleOnGesttureListener类,实现了OnGestureListener和OnDoubleTapListener接口,如果我们需要实现手势,只需要继承这个类,实现对应的手势方法即可。

    

示例:根据左右拖拽切换图片,直接实现接口

main.xml,添加一个ImageView显示图片,默认显示第一张图片

<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >

<ImageView
android:id="@+id/img_girl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/girl1" />

</LinearLayout>


main.java

package com.example.gesturedetectordemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnTouchListener, OnGestureListener {

//定义一个GestureDetector对象,用来处理各种手势,实现了OnTouchListener接口
GestureDetector myGestureDetector;
// 图片框
ImageView imggirl;
// 存放女孩图片的数组
int[] girls = { R.drawable.girl1, R.drawable.girl2, R.drawable.girl3, R.drawable.girl4, R.drawable.girl5 };
// 存放数组下标
private int index;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myGestureDetector = new GestureDetector(this);

imggirl = (ImageView) findViewById(R.id.img_girl);

//添加OnTouchListener事件
imggirl.setOnTouchListener(this);
//下面两个要记得设哦,不然就没法处理轻触以外的事件了,例如抛掷动作。
imggirl.setLongClickable(true);
myGestureDetector.setIsLongpressEnabled(true);
}

// 向后移动
public void goNext() {
index++;
index = Math.abs(index % girls.length);
imggirl.setImageResource(girls[index]);
}

// 向前移动
public void goPrevious() {
index--;
index = Math.abs(index % girls.length);
imggirl.setImageResource(girls[index]);
}

@Override  //处理用户的触碰请求
public boolean onTouch(View v, MotionEvent event) {
//转交给GestureDetector来处理
myGestureDetector.onTouchEvent(event);
return false;
}

@Override  //在按下动作时被调用
public boolean onDown(MotionEvent e) {
//goNext();
return false;
}

@Override  //按下动作松开被调用
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override  //在弹起时被调用
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override  //在滚动时被调用
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}

@Override   //在长按时被调用
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override  //在抛掷动作时被调用
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > 50) {
goNext();
Toast.makeText(MainActivity.this, "从右向左拖拽" + index, Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > 50) {
goPrevious();
Toast.makeText(MainActivity.this, "从左向右拖拽" + index, Toast.LENGTH_SHORT).show();
}
return false;
}

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐