gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:02 24 4
gpt4 key购买 nike

我刚开始开发 android 应用程序(使用 java,在 android studio 中,如果这很重要),我正在做一个小项目,只是为了好玩。我想创建自己的国际象棋应用程序,到目前为止我已经做了很多事情。我设置了一个菜单来切换到游戏本身的另一个 Activity ,我用一个自画板制作了一个自定义 View ,我认为我的模型也快完成了。我唯一不明白的是如何处理阻力。那么,当您使用拖动手势将一件棋子从一个位置移动到另一个位置时,您如何获得起点和终点?

如前所述,我已经在我的模型中实现了一个 Action (使用函数 move(Position start, Position end)),它还会检查该 Action 是否对某个棋子有效,但我唯一还需要的是可以让我在实际棋盘上拖动一 block 的东西。

我正在考虑在我的 Controller 类中放置一个 onDrag 方法,但我不知道如何解决这个问题,而且在 Internet 上也找不到好的示例。我已经开始这样做了,但不知道它是否可行。

你能帮我实现拖动吗?

提前致谢!

附言如果有帮助,我还将在我的问题中添加自定义 View 和(尚未完成) Controller 的代码。如果您需要我的更多代码来回答这个问题,我也会把它放在这里,请告诉我。

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

最佳答案

希望这样的事情能给你带来灵感:

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

引用:https://www.javacodegeeks.com/2011/12/android-drag-and-drop-tutorial.html

关于java - 如何处理Android国际象棋应用程序中的拖动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36888139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com