Android中触控单击、双击、长按、滑动效果

       手机屏幕事件的处理方法onTouchEvent。该方法在View类中的定义,并且所有的View子类全部重写了该方法,应用程序可以通过该方法处理手机屏幕的触摸事件。

        public boolean onTouchEvent(MotionEvent event)

       参数event:参数event为手机屏幕触摸事件封装类的对象,其中封装了该事件的所有信息,例如触摸的位置、触摸的类型以及触摸的时间等。该对象会在用户触摸手机屏幕时被创建。

        返回值:该方法的返回值机理与键盘响应事件的相同,同样是当已经完整地处理了该事件且不希望其他回调方法再次处理时返回true,否则返回false

        该方法并不像之前介绍过的方法只处理一种事件,一般情况下以下三种情况的事件全部由onTouchEvent方法处理,只是三种情况中的动作值不同。

        屏幕被按下:当屏幕被按下时,会自动调用该方法来处理事件,此时MotionEvent.getAction()的值为MotionEvent.ACTION_DOWN,如果在应用程序中需要处理屏幕被按下的事件,只需重新该回调方法,然后在方法中进行动作的判断即可。

        屏幕被抬起:当触控笔离开屏幕时触发的事件,该事件同样需要onTouchEvent方法来捕捉,然后在方法中进行动作判断。当MotionEvent.getAction()的值为MotionEvent.ACTION_UP时,表示是屏幕被抬起的事件。

        在屏幕中拖动:该方法还负责处理触控笔在屏幕上滑动的事件,同样是调用MotionEvent.getAction()方法来判断动作值是否为MotionEvent. ACTION _ MOVE 再进行处理。

       Android Touch Screen与传统Click Touch Screen不同,会有一些手势(Gesture),例如FlingScroll等等。这些Gesture会使用户体验大大提升。Android中的Gesture识别(detector)是通过GestureDetecto r.OnGesture Listener接口实现的。

       首先,Android事件处理机制是基于Listener实现的,比如触摸屏相关的事件,就是通过onTouchListener实现;其次,所有View的子类都可以通过setOnTouchListener()setOnKeyListener()等方法来添加对某一类事件的Listener;第三,Listener一般会以Interface的方式来提供,其中包含一个或多个abstract方法,我们需要实现这些方法来完成 onTouch()onKey()等操作。这样,程序便可以在特定的事件被dispatch到该view的时候,通过callback函数给予适当的响应。

1. Touch Screen Click举例

public class MyGesture extends Activity implements OnTouchListener 

     public void onCreate(Bundle savedInstanceState) 
     { 
          super.onCreate(savedInstanceState); 
 
          setContentView(R.layout.main); 
 
          TextView tv = (TextView) findViewById(R.id.tv); 
 
          tv.setOnTouchListener(this); 
      } 
     public boolean onTouch(View v, MotionEvent event) 
     { 
          Toast.makeText(this, "Touch Touch", Toast.LENGTH_SHORT).show(); 
 
          return false; 
      } 
 

        我们可以通过MotionEventgetAction()方法来获取Touch事件的类型,包括 ACTION_DOWN(按下触摸屏), ACTION_MOVE(按下触摸屏后移动受力点), ACTION_UP(松开触摸屏)和ACTION_CANCEL(不会由用户直接触发)。

2.当我们捕捉到Touch操作的时候,如何识别出用户的Gesture?这里我们需要GestureDetector.OnGestureListener接口的帮助,代码如下:
import android.os.Bundle;
import android.app.Activity;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyGesture extends Activity implements

OnTouchListener,android.view.GestureDetector.OnGestureListener,OnDoubleTapListener
 

 
    private GestureDetector mGestureDetector; 
 
    public MyGesture() 
 
    { 
 
        mGestureDetector = new GestureDetector(this); 
 
    } 
 
    public void onCreate(Bundle savedInstanceState) 
 
    { 
 
        super.onCreate(savedInstanceState); 
 
        setContentView(R.layout.activity_main); 
 
        ImageView tv=(ImageView)findViewById(R.id.imgview);
 
        tv.setOnTouchListener(this); 
 
        tv.setFocusable(true); 
 
        tv.setClickable(true); 
 
        tv.setLongClickable(true);  //必须的,这样才能处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),
 
        mGestureDetector.setIsLongpressEnabled(true); 
 
   } 
 
    /*       
     * *在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,
    * 将捕捉到的MotionEvent交给GestureDetector来分析是否有合适的callback函数来处理用户的手势
     */ 
 
    public boolean onTouch(View v, MotionEvent event) 
 
    { 
 
        return mGestureDetector.onTouchEvent(event); 
 
    } 
 
    // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发  
 
    public boolean onDown(MotionEvent arg0) 
 
    { 
 
        Log.i("MyGesture", "onDown");  
        Toast.makeText(this, "用户轻触触摸屏-->onDown", Toast.LENGTH_SHORT).show(); 
        this.setTitle("用户轻触触摸屏onDown");
        return true; 
 
    } 
 
    /*       
     * * 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发 *
     * 注意和onDown()的区别,强调的是没有松开或者拖动的状态
     */ 
 
    public void onShowPress(MotionEvent e) 
 
    { 
 
        Log.i("MyGesture", "onShowPress");  
        Toast.makeText(this, "用户轻触触摸屏,尚未松开或拖动-->onShowPress", Toast.LENGTH_SHORT).show(); 
        this.setTitle("onShowPress");
    } 
 
    // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发  
 
    public boolean onSingleTapUp(MotionEvent e) 
 
    { 
 
        Log.i("MyGesture", "onSingleTapUp"); 
        Toast.makeText(this, "用户(轻触触摸屏后)松开-->onSingleTapUp", Toast.LENGTH_SHORT).show(); 
        this.setTitle("用户(轻触触摸屏后)松开SingleTapUp");
        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");   
        Toast.makeText(this, "用户按下触摸屏、快速移动后松开-->onFling", Toast.LENGTH_LONG).show(); 
        this.setTitle("快速移动后松开onFling");
        return true; 
 
    } 
 
    // 用户按下触摸屏,并拖动,由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(); 
        this.setTitle("按下触摸屏,并拖动onScroll");
        return true; 
 
    } 
    // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发    
    public void onLongPress(MotionEvent e) 
 
   { 
 
        Log.i("MyGesture", "onLongPress");   
       Toast.makeText(this, "用户长按触摸屏-->onLongPress", Toast.LENGTH_LONG).show(); 
       this.setTitle("用户长按触摸屏onLongPress");
   } 
  
 

 @Override
 public boolean onDoubleTap(MotionEvent e) {
  // TODO Auto-generated method stub
  Log.i("MyGesture", "onDoubleTapUp"); 
    
                Toast.makeText(this, "用户双击屏幕-->onDoubleTap", Toast.LENGTH_SHORT).show(); 
                this.setTitle("用户双击屏幕onDoubleTap");
                return true; 
  
 }

 @Override
 public boolean onDoubleTapEvent(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
 }

 @Override
 public boolean onSingleTapConfirmed(MotionEvent e) {
               // TODO Auto-generated method stub
               return false;
 }
}

 

3.Fling事件的处理代码:除了第一个触发FlingACTION_DOWN和最后一个ACTION_MOVE中包含的坐标等信息外,我们还可以根据用户在X轴或者Y轴上的移动速度作为条件。比如下面的代码中我们就在用户移动超过100个像素,且X轴上每秒的移动速度大于200像素时才进行处理。

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
            float velocityY) 
 
    { 
 
        // 参数解释:  
 
        // 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(); 
 
        } 
 
        return false; 
 
    } 

这个例子中,tv.setLongClickable(true)是必须的,因为只有这样,view才能够处理不同于Tap(轻触)的hold(即ACTION_MOVE,或者多个ACTION_DOWN),我们同样可以通过layout定义中的android:longClickable来做到这一点.

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值