直接上代码了:

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);      }              /*      * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector      * 来分析是否有合适的callback函数来处理用户的手势      */       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;      }            /*      * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发      * 注意和onDown()的区别,强调的是没有松开或者拖动的状态      */      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. android手动拖动滚动条快速滑动
  2. 比如说我们要从用户表customer和用户订单表orders中,查询上海的用
  3. 实现内容精准化搜索和用户精准化推送的实例教程
  4. C#开发微信门户及应用(五)之用户分组信息管理
  5. C#开发微信门户及应用(四)之关注用户列表及详细信息管理
  6. Asp.net MVC 对用户输入的字符串做Trim处理的方法实例
  7. ASP.NET Core中用户登录验证实现最低配置的示例代码
  8. 用户管理和权限和设置——mysql
  9. c语言用户标识符命名规则是什么?

随机推荐

  1. Android(安卓)彻底征服 ListView 一 (实
  2. Android源码下载网址
  3. (转)Android(安卓)Toast用法
  4. Android(安卓)指纹笔记
  5. Android(安卓)启动异常:The connection to
  6. Android(安卓)Hander机制的理解
  7. 在 Android(安卓)的 IM 应用中使用 asmac
  8. Mars视频笔记——Socket编程
  9. android alertdialog
  10. [2010-12-31 21:33:29 - s] W/ResourceTy