如何在android中检测手指滑动

时间:2014-03-09 12:30:59

标签: android swipe ontouchevent

我正在尝试手指滑动,但未检测到。我正在尝试绘制一条描述手指滑动区域的路径,但只有当我用力滑动时才会滑动检测到,否则检测不到

public boolean onTouchEvent(MotionEvent event) {
if (System.currentTimeMillis() - lastClick > 500) {
       lastClick = System.currentTimeMillis();
       synchronized (getHolder()) {
           int eventaction = event.getAction();
           if(event.getPressure()>0.00001||event.getSize()>0.00001)
           {

           float a,b,c;
           switch(eventaction)
           {
           case MotionEvent.ACTION_DOWN:

               xdown = event.getX();
               ydown =event.getY();
               pres=true;

               break;

           case MotionEvent.ACTION_MOVE:
               if(pres)
               {

               xmove = event.getX();
               ymove =event.getY();
               a = (float) Math.sqrt(Math.pow(xdown - xmove,2) +Math.pow( ydown - ymove,2)); 
            b=(float) (xdown-(((xdown - xmove) / a) * 150));
            c=(float) (ydown-((( ydown - ymove)/a)*150));
            move.moveTo(xdown, ydown);
            move.lineTo(b, c);
               pres=false;
               lmove=true;

               }
               break;
           case MotionEvent.ACTION_CANCEL:
           case MotionEvent.ACTION_POINTER_UP:
               move.reset();
                   xmove = 0;
                   ymove =0;
                   xdown=0;
                   ydown=0;
               lmove=false;
               pres=false;
               break;
           default:
               return true;

           }
       }
       }}
return true;

       } 

2 个答案:

答案 0 :(得分:0)

Android有一个类。检查http://developer.android.com/reference/android/view/GestureDetector.html 我相信这是触摸事件之一

答案 1 :(得分:0)

请参阅此处的代码:Simple swipe gesture to activity tutorial?

使用SimpleOnGestureListener是最简单的,如果你覆盖onFling方法,很容易/

相关问题