import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
import android.widget.Toast;

public class GestureActivity extends Activity implements OnTouchListener,
        OnGestureListener {

GestureDetector detector;

public GestureActivity() {
        detector = new GestureDetector(this);
    }
   
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            TextView tv = (TextView) findViewById(R.id.TextView001);
            //设置tv的监听器
            tv.setOnTouchListener(this);
            tv.setFocusable(true);
            //必须,view才能够处理不同于Tap(轻触)的hold
            tv.setClickable(true);
            tv.setLongClickable(true);
            detector.setIsLongpressEnabled(true);
    }
   
   
      
    public boolean onTouch(View v, MotionEvent event) {
        return detector.onTouchEvent(event);
    }
 
    // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发
    public boolean onDown(MotionEvent arg0) {
        Log.i("MyGesture", "onDown");
        Toast.makeText(this, "onDown", Toast.LENGTH_SHORT).show();
        return true;
    }
     
     
    public void onShowPress(MotionEvent e) {
        Log.i("MyGesture", "onShowPress");
        Toast.makeText(this, "onShowPress", Toast.LENGTH_SHORT).show();
    }
     
    // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发
    public boolean onSingleTapUp(MotionEvent e) {
        Log.i("MyGesture", "onSingleTapUp");
        Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_SHORT).show();
        return true;
    }
     
    // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE, 1个ACTION_UP触发
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.i("MyGesture", "onFling");
       
        // 参数解释:
        // e1:第1个ACTION_DOWN MotionEvent
        // e2:最后一个ACTION_MOVE MotionEvent
        // velocityX:X轴上的移动速度,像素/秒
        // velocityY:Y轴上的移动速度,像素/秒
     
        // 触发条件 :
        // X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
         
        final int FLING_MIN_DISTANCE = 100, FLING_MIN_VELOCITY = 200;
        if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling left
            Log.i("MyGesture", "Fling left");
            Toast.makeText(this, "Fling Left", Toast.LENGTH_SHORT).show();
        } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
            // Fling right
            Log.i("MyGesture", "Fling right");
            Toast.makeText(this, "Fling Right", Toast.LENGTH_SHORT).show();
        } else if(e2.getY()-e1.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {
            // Fling down
            Log.i("MyGesture", "Fling down");
            Toast.makeText(this, "Fling down", Toast.LENGTH_SHORT).show();
        } else if(e1.getY()-e2.getY()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY) {
            // Fling up
            Log.i("MyGesture", "Fling up");
            Toast.makeText(this, "Fling up", Toast.LENGTH_SHORT).show();
        }
       
       
        return false;
        
    }
     
    // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.i("MyGesture", "onScroll");
        Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show();
        return true;
    }
     
    // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发
    public void onLongPress(MotionEvent e) {
        Log.i("MyGesture", "onLongPress");
        Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show();
    }

}

android GestureDetector 手势的判断的更多相关文章

  1. android GestureDetector 手势基础

    1. 当用户触摸屏幕的时候,会产生许多手势,例如down,up,scroll,filing等等,我们知道View类有个View.OnTouchListener内部接口,通过重写他的onTouch(Vi ...

  2. 看完这篇还不会 GestureDetector 手势检测,我跪搓衣板!

    引言 在 android 开发过程中,我们经常需要对一些手势,如:单击.双击.长按.滑动.缩放等,进行监测.这时也就引出了手势监测的概念,所谓的手势监测,说白了就是对于 GestureDetector ...

  3. Android 触摸手势基础 官方文档概览

    Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...

  4. Android 触摸手势基础 官方文档概览2

    Android 触摸手势基础 官方文档概览 触摸手势检测基础 手势检测一般包含两个阶段: 1.获取touch事件数据 2.解析这些数据,看它们是否满足你的应用所支持的某种手势. 相关API: Moti ...

  5. Android GestureDetector方法详解

    为了加强点击.拖动响应事件,Android提供了GestureDetector手势识别类.通过GestureDetector.OnGestureListener来获取当前被触发的操作手势(Single ...

  6. 怎样手势的判断android GestureDetector在android开发中

    import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view. ...

  7. android 上下左右手势判断 根据别人的改的

    GestureUtils.java package com.gesture; import android.content.Context;import android.util.DisplayMet ...

  8. GestureDetector手势检测

    Android手势检测器GestureDetector,要创建一个GestureDetector需要传入一个监听器GestureDetector.OnGestureListener. 案例(实现手机相 ...

  9. Android Gesture 手势创建以及使用示例

    在Android1.6的模拟器里面预装了一个叫Gestures Builder的程序,这个程序就是让你创建自己的手势的(Gestures Builder的源代码在sdk问samples里面有,有兴趣可 ...

随机推荐

  1. 研究一下uucode编码

    uucode编码是把任意二进制数据转换为ascii字符的编码用于在一些只能传递文本的地方传送二进制数据uu模块提供了encode()和decode()用于将一个文件转换为uucode编码的字符文件,文 ...

  2. Android Fragment用法之给Activity创建事件回调

    在某些案例中,可能需要Fragment与Activity共享事件.在Fragment内部定义一个回调接口是一个好方法,并且规定由持有它的Activity实现这个回调方法.当Activity通过接口接受 ...

  3. SQL 把数据从一张表更新到另一张表

    代码写多了,有些使用过的方法和技巧会一时半会想不起来,平日记录下来,方便自己和有需要的人日后查阅. UPDATE tb1 SET tb1.fieldOne = tb2.fieldOne /* 将原始表 ...

  4. 类似a:hover的伪类的注解

    a:link { font-size: 14pt; text-decoration: underline; color: blue; } /*设置a对象在未被访问前的样式表属性 .*/ a:hover ...

  5. Linux下动态链接库和静态链接库

    第一部分:编译过程 先了解一下linux下C代码的编译过程,C代码的编译,一般分成四个阶段,包括:预编译,编译,汇编和链接,这四个阶段的分工是 预处理过程,负责头文件展开,宏替换,条件编译的选择,删除 ...

  6. PreparedStatement可以有效地防止sql被注入

    import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import jav ...

  7. T-SQL事务实例

    begin try begin tran ,'); ; --RAISERROR ('Error raised in TRY block.',16,1); commit tran end try beg ...

  8. RazorEngine 学习笔记

    refer : https://github.com/Antaris/RazorEngine 微软的模板编辑器. Install-Package RazorEngine using RazorEngi ...

  9. 认识Android

    安卓的特点开放性平等性无界性方便性硬件的丰富性 Android操作系统之中,一共将体系结构划分为四层:应用层(Application).应用框架层(Application Framework).系统运 ...

  10. HTTP数据包头解析---之温故而知新!

    [转]HTTP请求模型和头信息参考 参考: http://blog.csdn.net/baggio785/archive/2006/04/13/661410.aspx模型: http://blog.c ...