GestureDetector和SimpleOnGestureListener的使用教程

本文详细介绍了如何通过Android平台实现各种手势识别与处理,包括轻触、滑动、长按等基本手势及其对应的事件处理方法。文章还提供了具体的代码示例,并强调了设置组件为可长按的重要性。

onSingleTapConfirmed()方法没有触发,是因为没有设置setLongClickable(true);

使用方法:

1.继承SimpleOnGestureListener

2.在onTouch()方法中,改变返回值detector.onTouchEvent(event);

3.在各个状态中,做出相应的处理。

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


附上另外一个讲的好的博客:

http://www.cnblogs.com/transmuse/archive/2010/12/02/1894833.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值