简体   繁体   English

如何处理android国际象棋应用程序中的拖动?

[英]How to handle drag in an android chess app?

I just started with developing android apps (with java, in android studio, if that matters), and I'm doing a little project, just for fun.我刚开始开发 android 应用程序(使用 java,在 android studio 中,如果这很重要的话),我正在做一个小项目,只是为了好玩。 I want to create my own chess app, and so far I have done quite a few things.我想创建自己的国际象棋应用程序,到目前为止我已经做了很多事情。 I set up a menu to switch to another activity which is the game itself, i made a customview with a self painted board, and I think my model is also almost complete.我设置了一个菜单以切换到另一个活动,即游戏本身,我使用自绘板制作了自定义视图,我认为我的模型也几乎完成了。 The only thing I don't understand is how to handle a drag.我唯一不明白的是如何处理阻力。 So, when you move one piece from a position to another position with a dragging gesture, how do you get the begin- and endpoints of that?那么,当您使用拖动手势将一块从一个位置移动到另一个位置时,您如何获得它的开始和结束点?

As said, I already implemented a move in my model(with a function move(Position start, Position end)), and it also checks if that move is valid for a certain piece, but the only thing I still need is something that lets me drag a piece on the actual board.如前所述,我已经在我的模型中实现了一个移动(使用函数 move(Position start, Position end)),它还检查该移动是否对某个棋子有效,但我仍然需要的唯一东西是让我在实际板上拖了一块。

I was thinking about putting an onDrag method in my Controller class, but I don't know how to work that out, and can't find good examples on the internet.我正在考虑在我的 Controller 类中放置一个 onDrag 方法,但我不知道如何解决这个问题,并且在互联网上找不到好的例子。 I already started with that, but don't know if it could ever work.我已经开始这样做了,但不知道它是否能奏效。

Could you please help me implementing the drag?你能帮我实现阻力吗?

Thanks in advance!提前致谢!

PS I will also add the code for the custom view and the (not yet complete)controller in my question, if that helps. PS,如果有帮助,我还将在我的问题中添加自定义视图和(尚未完成的)控制器的代码。 If you need more of my code to answer this question, I will also put it here, just let me know.如果你需要我的更多代码来回答这个问题,我也会把它放在这里,让我知道。

public class ChessView extends View implements Observer {
    private Game game;
    private static final Paint WHITE_PAINT = new Paint(), BLACK_PAINT = new Paint();

    public ChessView(Context context) {
        super(context);
        init();
    }

    public ChessView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ChessView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public void init() {
        WHITE_PAINT.setColor(Color.rgb(200, 159, 77));
        BLACK_PAINT.setColor(Color.rgb(61, 34, 18));
    }

    public void setGame(Game game) {
        if (this.game != null)
            this.game.deleteObserver(this);

        this.game = game;
        this.game.addObserver(this);
    }

    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (game == null)
            return;

        drawBoard(canvas);
        drawPieces(canvas);
    }

    public void drawBoard(Canvas canvas) {
        int tilesize = Math.min(getWidth(), getHeight())/8;

        for (int i = 0; i < 8; i++)
            for (int j = 0; j < 8; j++) {
                Paint paint = ((i + j) % 2 == 0) ? WHITE_PAINT : BLACK_PAINT;

                canvas.drawRect(i*tilesize, j*tilesize,(i+1)*tilesize, (j+1)*tilesize, paint);
            }
    }

    public void drawPieces(Canvas canvas) {
        for (int i = 0; i < game.getBoard().boardSize(); i++)
            for (int j = 0; j < game.getBoard().boardSize(); j++) {
                Position pos = new Position(i, j);
                Piece p = game.getBoard().getPiece(pos);

                if (p != null)
                    drawPiece(canvas, p, pos);
                else
                    clearPos(canvas, pos);
            }
    }

    public void drawPiece(Canvas canvas, Piece piece, Position position) {
        switch (game.getBoard().getPiece(position).getId()) {
            case ("wpawn"): drawPicture(canvas, position, R.drawable.wpawn); break;
            case ("bpawn"): drawPicture(canvas, position, R.drawable.bpawn); break;
            case ("wrook"): drawPicture(canvas, position, R.drawable.wrook); break;
            case ("brook"): drawPicture(canvas, position, R.drawable.brook); break;
            case ("wknight"): drawPicture(canvas, position, R.drawable.wknight); break;
            case ("bknight"): drawPicture(canvas, position, R.drawable.bknight); break;
            case ("wbishop"): drawPicture(canvas, position, R.drawable.wbishop); break;
            case ("bbishop"): drawPicture(canvas, position, R.drawable.bbishop); break;
            case ("wqueen"): drawPicture(canvas, position, R.drawable.wqueen); break;
            case ("bqueen"): drawPicture(canvas, position, R.drawable.bqueen); break;
            case ("wking"): drawPicture(canvas, position, R.drawable.wking); break;
            case ("bking"): drawPicture(canvas, position, R.drawable.bking); break;
            default: break;
        }
    }

    public void drawPicture(Canvas canvas, Position position, int picture) {
        int tilesize = Math.min(getHeight(), getWidth())/8, x = position.getY(), y = position.getX();
        Drawable d = ResourcesCompat.getDrawable(getResources(), picture, null);
        Bitmap b = ((BitmapDrawable) d).getBitmap();

        canvas.drawBitmap(b, null, new Rect(x*tilesize, y*tilesize,(x + 1)*tilesize, (y + 1)*tilesize), null);
    }

    public void clearPos(Canvas canvas, Position position) {
        int tilesize = Math.min(getWidth(), getHeight())/8, x = position.getY(), y = position.getX();

        Paint paint = ((position.getX() + position.getY()) % 2 == 0) ? WHITE_PAINT : BLACK_PAINT;

        canvas.drawRect(x*tilesize, y*tilesize, (x + 1)*tilesize, (y + 1)*tilesize, paint);
    }

    @Override
    public void update(Observable observable, Object data) {
        this.postInvalidate();
    }
}

public class Controller extends Observable implements View.OnDragListener {
    private Game game;

        public Controller(Game game) {
            this.game = game;
        }

        @Override
        public boolean onDrag(View v, DragEvent event) {

            float startx = event.getX();
            float starty = event.getY();

            if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {

            }

            return false;
        }
    }

Hopefully something like this would give you the idea:希望这样的事情会给你这个想法:

@Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ...               
            view.startDrag(clipData, dsb, view, 0);
            ...
            return true;
        } else {
            return false;
        }
    }

 @Override
    public boolean onDrag(View view, DragEvent dragEvent) {
        int dragAction = dragEvent.getAction();
        View dragView = (View) dragEvent.getLocalState();
        if (dragAction == DragEvent.ACTION_DRAG_EXITED) {
            containsDragable = false;
        } else if (dragAction == DragEvent.ACTION_DRAG_ENTERED) {
            containsDragable = true;
        } else if (dragAction == DragEvent.ACTION_DROP && containsDragable){
            //your function to move and check valid moves
            dragView.setVisibility(View.VISIBLE);
        }
        return true;
    }

Ref: https://www.javacodegeeks.com/2011/12/android-drag-and-drop-tutorial.html参考: https : //www.javacodegeeks.com/2011/12/android-drag-and-drop-tutorial.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM