Android多点触控背景在第二个指针上跳跃

时间:2015-03-09 14:24:06

标签: android canvas multi-touch translate

我显然遗漏了一些大而简单的东西,但我无法找到它。我有一个带有背景图像的表面视图,几个移动/可移动的精灵,并且可以缩放和平移图像。一切正常......除非在翻译画布时用第二个指针触摸。

如果画布是0,0,没问题。如果画布已翻译,则第一个指针没有问题。但是,如果第二次手指接触,它会捕捉到0,0。抬起第二根手指,然后快速向后翻转。

看起来像一个简单的修复 - 如果第二个指针,保持翻译值。但这就是问题 - 日志显示它 保持tx值 - 它从不在任何地方显示0,0。

我尝试过使用if (activePointer != INVALID_POINTER_ID)方法&它做同样的事情。当日志显示翻译值没有改变时,我只能弄清楚是什么告诉画布跳转到0,0。

以下是我的许多头痛尝试中的一个示例:

boolean doTouchEvent(MotionEvent ev) { 
        synchronized (gvSurfaceHolder) {
            mScaleDetector.onTouchEvent(ev);    
            float x = ev.getX(0);
            float y = ev.getY(0);

         // get pointer index from the event object
            int pointerIndex = ev.getActionIndex();

            // get pointer ID
            int pointerId = ev.getPointerId(pointerIndex);

            switch (ev.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:    
                    contact = false;            
                    // mLastTouch handles the finger position for moving the sprites
                    // translate handles where to move the whole screen when NOT touching sprites

                    mLastTouchX = x / mScaleFactor;
                    mLastTouchY = y / mScaleFactor;

                  //We assign the current X and Y coordinate of the finger to startX and startY minus the previously 
                  //translated amount for each coordinates This works even when we are translating the first 
                  //time because the initial values for these two variables is zero.              
                  startX = x - previousTranslateX;
                  startY = y - previousTranslateY;      

                  detectPCTouch();
                  if (contact){
                     mLastTouchX = startX / mScaleFactor;
                     mLastTouchY = startY / mScaleFactor;
                  }testVarDump("Down", pointerIndex, pointerId);        
                  break;

                case MotionEvent.ACTION_POINTER_DOWN:   
                    stopTranslate = true;
                     testVarDump("ACTION Pointer DOWN", pointerIndex, pointerId);
                    break;

                case MotionEvent.ACTION_MOVE:                   
                    x = ev.getX(0);
                    y = ev.getY(0);
                    //testVarDump("ACTION Move", pointerIndex, pointerId);
                    // Only move if the ScaleGestureDetector isn't processing a gesture.
                    if (!mScaleDetector.isInProgress()){
                        if (!contact){
                        translateX = x - startX;
                        translateY = y - startY;
                        mLastTouchX = x / mScaleFactor;
                        mLastTouchY = y / mScaleFactor;
                        }else{
                            translateX = previousTranslateX;
                            translateY = previousTranslateY;
                            mLastTouchX = (x - previousTranslateX) / mScaleFactor;
                            mLastTouchY = (y - previousTranslateY) / mScaleFactor;
                        }
                    }                       
                    break;  

                case MotionEvent.ACTION_POINTER_UP: 
                    contact = false;
                    stopTranslate = true;
                    testVarDump("ACTION Pointer UP", pointerIndex, pointerId);
                    break;

                case MotionEvent.ACTION_UP:                 
                    // All fingers went up, so let's save the value of translateX and translateY into 
                    // previousTranslateX and previousTranslate   
                    if (!contact){                  
                        previousTranslateX = translateX;
                        previousTranslateY = translateY;
                    }else{  
                        translateY = previousTranslateY;
                        translateX = previousTranslateX;
                    }

                    contact = false;
                    stopTranslate = false;testVarDump("ACTION Up", pointerIndex, pointerId);
                    break;

            }               
        }
        return true;
    }

运行块:

@Override
    public void run() {
        while (running) {
            Canvas c = null;
            try {
                c = gvSurfaceHolder.lockCanvas(null);

                synchronized (gvSurfaceHolder) {                        
                    c.save();
                    c.scale(mScaleFactor, mScaleFactor);
                    //If translateX times -1 is lesser than zero, let's set it to zero. This takes care of the left bound
                    if((translateX * -1) < 0) 
                        translateX = 0;             
                    //This is where we take care of the right bound. We compare translateX times -1 to (scaleFactor - 1) * displayWidth.
                    //If translateX is greater than that value, then we know that we've gone over the bound. So we set the value of
                    //translateX to (1 - scaleFactor) times the display width. Notice that the terms are interchanged; it's the same
                    //as doing -1 * (scaleFactor - 1) * displayWidth
                    else if((translateX * -1) > (mScaleFactor - 1) * screenW) 
                        translateX = (1 - mScaleFactor) * screenW;                                          
                    if(translateY * -1 < 0) 
                        translateY = 0;                 
                    //We do the exact same thing for the bottom bound, except in this case we use the height of the display
                    else if((translateY * -1) > (mScaleFactor - 1) * screenH) 
                        translateY = (1 - mScaleFactor) * screenH;                  

                    if (!mScaleDetector.isInProgress()) 
                        c.translate(translateX / mScaleFactor, translateY / mScaleFactor);

      // Here I'd expect translateX to be 0, but always logs as correctly
      // translated, even when the background snaps to 0,0 with second pointer touch
                    if (stopTranslate)
                        testVarDump("DRAW", 0 ,0); 

                    updateBadGuys();
                    detectCollision();
                    draw(c);
                    c.restore();
                }
            } finally {
                if (c != null) 
                    gvSurfaceHolder.unlockCanvasAndPost(c);
            }
        }

我在这里有点疯狂 - 任何想法当第二根手指上升或下降时是什么迫使翻译?请帮忙 - 我错过了什么?

0 个答案:

没有答案