1

我们在一个按钮上有一个长按检测。检测到时,应关闭视图(返回游戏视图)并且使用按钮选择的对象应位于长按的坐标处。那是容易的部分,困难的部分是然后用手指改变坐标,而不释放它。

好的,所以我正在尝试重新表述这一点:我们有两个视图显示在彼此之上。我们想要的是在顶视图中检测到长按,然后关闭顶视图,并且在不释放手指的情况下,在底视图中获取触摸事件。

这是我们的gameView类:(这段代码包含很多代码unneccesay的问题)

@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;
}

这是手势处理程序:

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

很抱歉最终缺乏信息或解释不好:)

4

0 回答 0