Android_gesturedetector

xml:

<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.android_gesturedetector.MainActivity" >
    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"/>
</RelativeLayout>

main.java

package com.example.android_gesturedetector;

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;
/*
 * 使用GestureDetector进行手势识别
 * 手势交互过程(原理):
 * 1.触屏一刹那,触发MotionEvent事件(移动事件)
 * 2.被OnTouchListener监听,在onTouch()中获得MotionEvent对象
 * 3.GestureDetector(手势识别器)转发MotionEvent对象至OnGestureListener
 * 4.OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈
 *
 *
 *
 * MotionEvent:
 * 1.用于封装手势,触摸笔,轨迹球等动作事件
 * 2.内部封装用于记录横轴和纵轴坐标的属性x和y
 *
 * GestureDetector:(手势识别器)
 * 识别各种手势
 *     工作原理:
 *     1.当接收到用户触摸消息时 ,将消息交给GestureDetector加工
 *     2.通过设置监听器获得GestureDetector处理后的手势
 * GestureDetector提供了两个接口
 *     OnGestureListener:(是被单击事件)
 *         1.手势交互的监听接口,其提供多个抽象方法
 *         2.根据GestureDetector的手势识别结果调用相对应的方法
 *     OnDoubleTapListener.
 * SimpleOnGeistureListener实现了上面两种接口
 *     1.继承SimpleOnGestureListener
 *     2.可以只重载感兴趣的手势
 */
public class MainActivity extends Activity {
    private ImageView image;
    private 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);
        image = (ImageView) findViewById(R.id.image);
        myGestureDetector = new GestureDetector(new MyGestureListener());//实例化GestureDetector
        image.setOnTouchListener(new OnTouchListener() {

            @Override//可以捕获到触摸屏幕发生的event事件
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                myGestureDetector.onTouchEvent(event);//GestureDetector对象通过onTouchEvent方法将event事件转发给GestureListener接口的实现类
                return true;
            }
        });

    }

}
时间: 2024-08-28 05:30:30

Android_gesturedetector的相关文章

Android_GestureDetector手势滑动使用

Gesture在 ViewGroup中使用 GestureDetector类可以让我们快速的处理手势事件,如点击,滑动等. 使用GestureDetector分三步: 1. 定义GestureDetector类 2. 初始化手势类,同时设置手势监听 3. 将touch事件交给gesture处理 先来了解一下如何使用,后面会有示例: package com.example.y2222.myview; import android.content.Context; import android.ut

maven dependendency

登录|注册     zhengsj的专栏 目录视图 摘要视图 订阅 [公告]博客系统优化升级     [收藏]Html5 精品资源汇集     博乐招募开始啦 Maven Dependency设置,详解! 标签: mavenhibernatejargooglenetbeansjbuilder 2009-09-25 10:18 30736人阅读 评论(6) 收藏 举报 分类: 工具文章(4) come from : http://www.javaeye.com/topic/240424 用了Mav