将长按从一个安卓视图转移到另一个视图,而不释放手指

时间:2012-12-02 17:05:48

标签: android view gesture

我们在按钮上进行长按检测。检测到时,应取消视图(返回游戏视图),使用按钮选择的对象应位于longpress的坐标处。这是最容易的部分,困难的部分是用手指改变坐标,而不释放它。

好的,所以我正在尝试重新制定这个:我们有两个视图显示在彼此之上。我们想要的是在顶视图中检测到的长按,然后关闭顶视图,而不释放手指,在底部视图中获取touchevents。

这是我们的类gameView :(此代码包含大量不需要的代码)

@Override
public boolean onTouchEvent(MotionEvent event) {

    gestureDetector.onTouchEvent(event);

    switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN:
            mode = DRAG;
            //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 = event.getX() - previousTranslateX;
            startY = event.getY() - previousTranslateY;
            break;

        case MotionEvent.ACTION_MOVE:               
            translateX = event.getX() - startX;
            translateY = event.getY() - startY;

            gestureHandler.setTranslation(translateX, translateY);

            //We cannot use startX and startY directly because we have adjusted their values using the previous translation values. 
            //This is why we need to add those values to startX and startY so that we can get the actual coordinates of the finger.
            double distance = Math.sqrt(Math.pow(event.getX() - (startX + previousTranslateX), 2) + 
                                        Math.pow(event.getY() - (startY + previousTranslateY), 2)
                                       );

            if(distance > 0) {
               dragged = true;
            }               

            break;

        case MotionEvent.ACTION_POINTER_DOWN:
            mode = ZOOM;

            scaleToX = event.getX();
            scaleToY = event.getY();
            break;

        case MotionEvent.ACTION_UP:
            mode = NONE;
            dragged = false;

            //All fingers went up, so let's save the value of translateX and translateY into previousTranslateX and 
            //previousTranslate           
            previousTranslateX = translateX;
            previousTranslateY = translateY;

            int x = (int)((-translateX/scaleFactorX)+(event.getX()/this.scaleFactorX));
            int y = (int)((-translateY/scaleFactorY)+(event.getY()/this.scaleFactorY));

            map.click(getContext(), x, y);


            for(CastleTile castleTile : map.getCastleTiles()) {

                if(castleTile.getRect().contains((int)((-translateX/scaleFactorX)+(event.getX()/this.scaleFactorX)), (int)((-translateY/scaleFactorY)+(event.getY()/this.scaleFactorY)))){
                    Log.d("Castle to your service", "Boes");

                    castleSettings.show();
                }
            }
            break;

        case MotionEvent.ACTION_POINTER_UP:
            mode = DRAG;

            //This is not strictly necessary; we save the value of translateX and translateY into previousTranslateX
            //and previousTranslateY when the second finger goes up
            previousTranslateX = translateX;
            previousTranslateY = translateY;
            break;       
    }

    detector.onTouchEvent(event);

    //We redraw the canvas only in the following cases:
    //
    // o The mode is ZOOM 
    //        OR
    // o The mode is DRAG and the scale factor is not equal to 1 (meaning we have zoomed) and dragged is
    //   set to true (meaning the finger has actually moved)
    if ((mode == DRAG && scaleFactorX != 1f && scaleFactorY != 1f && dragged) || mode == ZOOM) {
        invalidate();
    }

    return true;
}

@Override
public boolean onLongClick(View v) {
    castleSettings.dismiss();
    return true;
}

这就是gestureHandler:

public void onLongPress(MotionEvent event) {
    map.longClick(context, (int)((-translateX/scaleFactorX)+(event.getX()/this.scaleFactorX)), (int)((-translateY/scaleFactorY)+(event.getY()/this.scaleFactorY)));
}

很抱歉最终缺乏信息或说明不好:)

0 个答案:

没有答案