在没有ScaleGestureDetector的情况下检测缩放和转换手势

时间:2012-11-11 10:07:33

标签: android scale multi-touch gesture translate

我尝试为API级别7实现多点触摸手势检测。这意味着我没有ScaleGestureDetector。目前我有这样的事情,但它不能很好地工作,我有更多的开放性问题,而不是完全理解:

public boolean onTouchEvent(MotionEvent ev) 
{
   final int action = ev.getAction();
   switch (action & MotionEvent.ACTION_MASK) // why mask it with ACTION_MASK?
   {
      case MotionEvent.ACTION_DOWN: 
      {
         mLastTouchX=ev.getX();
         mLastTouchY=ev.getY();
         mActivePointerId = ev.getPointerId(0);
         break;            
      }
     case MotionEvent.ACTION_POINTER_DOWN: 
     {
        mLastTouchX2=ev.getX();
        mLastTouchY2=ev.getY();
        if (ev.getPointerCount()>1)
         mActivePointerId2 = ev.getPointerId(1);
        break;
     }
     case MotionEvent.ACTION_MOVE: 
     {
        int   pointerIndex;
        float x=0.0f,y=0.0f;

        try
        {
           if ((mActivePointerId!=INVALID_POINTER_ID) || (mActivePointerId2!=INVALID_POINTER_ID))
           {
              // get one of the active pointers - unfortunately here I'm not sure which one is the active one so I only can guess
              pointerIndex= ev.findPointerIndex(mActivePointerId);
              x= ev.getX(pointerIndex);
              y= ev.getY(pointerIndex);
           }
           if ((mActivePointerId!=INVALID_POINTER_ID) && (mActivePointerId2!=INVALID_POINTER_ID))
           {
              float d1,d2;

              pointerIndex = ev.findPointerIndex(mActivePointerId2);
              if (pointerIndex<0) return false;
              float x2 = ev.getX(pointerIndex);
              float y2 = ev.getY(pointerIndex);

              d1=android.util.FloatMath.sqrt((x-x2)*(x-x2)+(y-y2)*(y-y2));
              d2=android.util.FloatMath.sqrt((mLastTouchX-mLastTouchX2)*(mLastTouchX-mLastTouchX2)+
                                  (mLastTouchY-mLastTouchY2)*(mLastTouchY-mLastTouchY2));                  

              if ((d1>0) && (d2>0)) // seems to be a scale gesture with two pointers
              {
                 float w,h,s;

                 transOffsetX=0.0f;
                 transOffsetY=0.0f;
                 s=d1/d2;
                 mScaleFactor*=s;
                 matrix.postScale(s,s);
                 w=(scrWidth-(scrWidth*s))/2.0f;
                 h=(scrHeight-(scrHeight*s))/2.0f;

                 matrix.postTranslate(w,h);
                 imgOffsetX+=w;
                 imgOffsetY+=h;                                    
              }

              mLastTouchX2 = x2;
              mLastTouchY2 = y2;
           }
           else if (mScaleFactor==1.0) // seems to be a translate gesture with only one pointer
           {
              mScaleFactor=1.0f;
              transOffsetX+=(x-mLastTouchX);
              transOffsetY+=(y-mLastTouchY);
              matrix.setTranslate(transOffsetX,transOffsetY);
           }

           if ((mActivePointerId!=INVALID_POINTER_ID) || (mActivePointerId2!=INVALID_POINTER_ID))
           {             
              mLastTouchX = x;
              mLastTouchY = y;
           }
        }
        catch (ArrayIndexOutOfBoundsException aioobe)
        {
           // this is really strange, this exception can be caused by
           // pointerIndex= ev.findPointerIndex(mActivePointerId);
           // x= ev.getX(pointerIndex);
           // above which seems to be a Android bug?
        }
        break;
     }
     case MotionEvent.ACTION_UP: 
     case MotionEvent.ACTION_POINTER_UP: 
     {
        breakMapThread=true;
        mActivePointerId = INVALID_POINTER_ID;
        mActivePointerId2 = INVALID_POINTER_ID;

        // gestrue seems to be finished so trigger update of the view here
        ...

        break;
     }
  }      
  return true;

}

整件事情真的很糟糕。缩放手势会导致较高的额外翻译,单击视图也会导致翻译,翻译不是很准确。除此之外,我发现了一些似乎永远不会被使用的运动事件常数ACTION_POINTER_1 / 2 / 3_DOWN / UP。所以我绝对不确定我的整个作业是否从指针1和2形成_DOWN / _UP是正确的。

有任何想法,提示和技巧可以使这件事发挥作用吗?

1 个答案:

答案 0 :(得分:0)

一个想法是找到ScaleGestureDetector的源并将其包含在您的项目中。 但是存在一些问题:有时系统会检测到点击和长按(特别是当手指离开窗口边界时)。