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();
    }

}

最新文章

  1. 【Java每日一题】20161213
  2. linux命令行中单个减号表示标准输入输出流
  3. MongoDB简介与增删改查
  4. Python 集合方法总结
  5. jQuery中的siblings
  6. Fully differential amplifiers
  7. LSP“浏览器劫持概念
  8. 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
  9. 【笔记】Unix 平台标准
  10. Mysql编译安装及优化
  11. 基础概念PHP-FPM、FastCGI和CGI
  12. Carthage 让项目支持及使用,第三方静态库转为动态库
  13. Python 类的内置方法
  14. 从Linux内核中获取真随机数【转】
  15. sqlserver配置允许快照隔离
  16. MySQL数据库之part1
  17. _lottery
  18. stark组件之过滤操作【模仿Django的admin】
  19. WinForm中变Enter键为Tab键实现焦点转移的方法
  20. zw版【转发·台湾nvp系列Delphi例程】HALCON union1

热门文章

  1. 关于在repeater中的checkbox实行多选和全选
  2. CentOS重启与关机
  3. 对于没有Command属性时,怎么来达到相同的效果
  4. php视图操作
  5. Android 用ListView实现GridView分列显示
  6. HDU 4614 (13年多校第二场1004)裸线段树
  7. 返回 m 到 n 的随机整数
  8. nuc950支持nand的mtd驱动的kernel修改
  9. 一个Div在BOdy中上下左右居中
  10. 在线提取PDF中图片和文字