1

ボタンの長押し検出があります。検出された場合、ビューは閉じられ(ゲームビューに戻る)、ボタンで選択されたオブジェクトは長押しの座標にある必要があります。それは簡単な部分ですが、難しい部分は、指を離さずに指で座標を変更することです。

さて、私はこれを再定式化しようとしています。2つのビューが互いに重ねて表示されています。必要なのは、上面図でロングクリックが検出され、上面図を閉じ、指を離さずに底面図でタッチイベントを取得することです。

これは私たちのクラス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)));
}

情報の不足や説明の誤りでごめんなさい:)

4

0 に答える 0