ACTION_MOVE returns 每次在 OnTouch 中调用时都是相同的值

ACTION_MOVE returns the same value every time it is called in OnTouch

每次 ACTION_MOVE 被调用时,我都试图调用一个方法来更新具有不同值的 tts 元素。但是,它 returns 每次调用时都具有相同的值。 respondCoordinates(v) 方法有一个 if 语句来更改值。

public boolean onTouch(View v, MotionEvent event) {
    //_gestureDetector.onTouchEvent(event);

    int eventAction = event.getAction();

    // you may need the x/y location
    int x = (int)event.getRawX();
    int y = (int)event.getRawY();

    Point boardPos = getBoardIndexX(new Point(x,y));
    // put your code in here to handle the event
    switch (eventAction) {
        case MotionEvent.ACTION_DOWN:

            Log.d("Chess", "Down: " + x + ", " + y + " [" + boardPos.x + ", " + boardPos.y + "]");
            if(OnBoard(boardPos))
            {
                // then play
                lastPlayTime = System.currentTimeMillis();
                lastPlayPos.x = boardPos.x;
                lastPlayPos.y = boardPos.y;
                respondCoordinates(v);

            }

            break;//return true;
        case MotionEvent.ACTION_UP:
            Log.d("Chess", "Up: " + x + ", " + y + " [" + boardPos.x + ", " + boardPos.y + "]");
            lastPlayPos.x = -1;
            lastPlayPos.y = -1;

            v.performClick();
            break;//return true;
        case MotionEvent.ACTION_MOVE:
            if(boardPos.x != lastPlayPos.x || boardPos.y != lastPlayPos.y) {
                // then we're on a new square
                Log.d("Chess", "Move: " + x + ", " + y + " [" + boardPos.x + ", " + boardPos.y + "]");
                lastPlayPos.x = boardPos.x;
                lastPlayPos.y = boardPos.y;

                if(OnBoard(boardPos))
                {
                    // then play
                    lastPlayTime = System.currentTimeMillis();
                    respondCoordinates(v);
                }
            }
    }
    // tell the View to redraw the Canvas
    //invalidate();

    return true;//super.onTouchEvent(event);
}

public void respondCoordinates(View v) {

    int iTo = getIndexOfButton(v);

    String join = "";
    String letter = "";
    String number = "";
    String piece = "";


    if ((iTo >= 0) && (iTo <= 7)) {
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }
        number = "8";
        if (iTo == 0) {
            letter = "a";


        } else if (iTo == 1) {
            letter = "b";
        } else if (iTo == 2) {
            letter = "c";
        } else if (iTo == 3) {
            letter = "d";

        } else if (iTo == 4) {
            letter = "e";

        } else if (iTo == 5) {
            letter = "f";

        } else if (iTo == 6) {
            letter = "g";

        } else if (iTo == 7) {
            letter = "h";

        }

    } else if ((iTo >= 8) && (iTo <= 15)) {
        number = "7";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 8) {
            letter = "a";
        } else if (iTo == 9) {
            letter = "b";
        } else if (iTo == 10) {
            letter = "c";
        } else if (iTo == 11) {
            letter = "d";
        } else if (iTo == 12) {
            letter = "e";
        } else if (iTo == 13) {
            letter = "f";
        } else if (iTo == 14) {
            letter = "g";
        } else if (iTo == 15) {
            letter = "h";
        }


    } else if ((iTo >= 16) && (iTo <= 23)) {
        number = "6";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 16) {
            letter = "a";
        } else if (iTo == 17) {
            letter = "b";
        } else if (iTo == 18) {
            letter = "c";
        } else if (iTo == 19) {
            letter = "d";
        } else if (iTo == 20) {
            letter = "e";
        } else if (iTo == 21) {
            letter = "f";
        } else if (iTo == 22) {
            letter = "g";
        } else if (iTo == 23) {
            letter = "h";
        }


    } else if ((iTo >= 24) && (iTo <= 31)) {
        number = "5";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 24) {
            letter = "a";
        } else if (iTo == 25) {
            letter = "b";
        } else if (iTo == 26) {
            letter = "c";
        } else if (iTo == 27) {
            letter = "d";
        } else if (iTo == 28) {
            letter = "e";
        } else if (iTo == 29) {
            letter = "f";
        } else if (iTo == 30) {
            letter = "g";
        } else if (iTo == 31) {
            letter = "h";
        }


    } else if ((iTo >= 32) && (iTo <= 39)) {
        number = "4";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 32) {
            letter = "a";
        } else if (iTo == 33) {
            letter = "b";
        } else if (iTo == 34) {
            letter = "c";
        } else if (iTo == 35) {
            letter = "d";
        } else if (iTo == 36) {
            letter = "e";
        } else if (iTo == 37) {
            letter = "f";
        } else if (iTo == 38) {
            letter = "g";
        } else if (iTo == 39) {
            letter = "h";
        }


    } else if ((iTo >= 40) && (iTo <= 47)) {
        number = "3";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 40) {
            letter = "a";
        } else if (iTo == 41) {
            letter = "b";
        } else if (iTo == 42) {
            letter = "c";
        } else if (iTo == 43) {
            letter = "d";
        } else if (iTo == 44) {
            letter = "e";
        } else if (iTo == 45) {
            letter = "f";
        } else if (iTo == 46) {
            letter = "g";
        } else if (iTo == 47) {
            letter = "h";
        }


    } else if ((iTo >= 48) && (iTo <= 55)) {
        number = "2";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 48) {
            letter = "a";
        } else if (iTo == 49) {
            letter = "b";
        } else if (iTo == 50) {
            letter = "c";
        } else if (iTo == 51) {
            letter = "d";
        } else if (iTo == 52) {
            letter = "e";
        } else if (iTo == 53) {
            letter = "f";
        } else if (iTo == 54) {
            letter = "g";
        } else if (iTo == 55) {
            letter = "h";
        }


    } else if ((iTo >= 56) && (iTo <= 63)) {
        number = "1";
        if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.PAWN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.PAWN) {
            piece = "Pawn";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.BISHOP || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.BISHOP) {
            piece = "Bishop";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.ROOK || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.ROOK) {
            piece = "Rook";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KNIGHT || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KNIGHT) {
            piece = "Knight";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.QUEEN || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.QUEEN) {
            piece = "Queen";
        } else if (_jni.pieceAt(BoardConstants.WHITE, iTo) == BoardConstants.KING || _jni.pieceAt(BoardConstants.BLACK, iTo) == BoardConstants.KING) {
            piece = "King";
        }

        if (iTo == 56) {
            letter = "a";
        } else if (iTo == 57) {
            letter = "b";
        } else if (iTo == 58) {
            letter = "c";
        } else if (iTo == 59) {
            letter = "d";
        } else if (iTo == 60) {
            letter = "e";
        } else if (iTo == 61) {
            letter = "f";
        } else if (iTo == 62) {
            letter = "g";
        } else if (iTo == 63) {
            letter = "h";
        }
    }

    join = letter + number + "" + piece;

    //Log.d("Chess", join);

    _chessActivity.soundNotification(join);


}


public int getIndexOfButton(View but){
    for(int i = 0; i < 64; i++){
        if(_arrImages[i] == ((ChessImageView)but)){

            _arrImages[i].setPressed(false);
            return i;
        }
    }
    return -1;
}

这需要检查,但我相信如果您在 ACTION_UP 之前离开一个视图,您仍然会得到那个视图的 ACTION_MOVE,所以您的视图 v 始终是您拥有的视图你的 ACTION_DOWN 继续,即使你继续另一个。

这可能是预期的行为。

我认为 this 可能会证实这一点。不过,他们的解决方案可能不适合您的情况,因为您会得到您触摸的所有视图的输出,而不仅仅是当前视图。

这很烦人,但我认为解决方案是在网格上而不是单元格上设置一个侦听器,并从 x 和 y 中找到您正在触摸的单元格。

我想我对问题的理解是正确的。您假设传递给 onTouchView 是棋盘上 64 个视图之一,但事实并非如此。传递给 onTouchView 是您设置 OnTouchListenerView。在这种情况下,它是棋盘本身。

相反,您应该使用 MotionEvent 的坐标来查找单个片段的索引。该方法应如下所示:

private int mLastChildIndex = -1;

public boolean onTouch(View view, MotionEvent event) {
    int x = (int) event.getRawX();
    int y = (int) event.getRawY();

    switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mLastChildIndex = getChildIndex((ViewGroup) view, x, y);
            playNotificationForIndex(mLastChildIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            int currentIndex = getChildIndex((ViewGroup) view, x, y);
            if (currentIndex != mLastChildIndex) {
                playNotificationForIndex(currentIndex);
                mLastChildIndex = currentIndex;
            } break;
        case MotionEvent.ACTION_UP:
            performClickForIndex(mLastChildIndex);
            mLastChildIndex = -1;
            break;
    }

    return true; // This method eats all MotionEvent actions for the View
}

private int getChildIndex(ViewGroup viewGroup, int x, int y) {
    for (int i = 0; i < viewGroup.getChildCount(); i++) {
        View child = viewGroup.getChildAt(i);

        int[] coordinates = new int[2];
        child.getLocationOnScreen(coordinates);

        Rect bounds = new Rect(coordinates[0], coordinates[1],
                coordinates[0] + child.getWidth(), coordinates[1] + child.getHeight());
        if (bounds.contains(x, y)) {
            return i;
        }
    } return -1;
}