2016-04-27 18 views
7

我剛開始開發android應用程序(在java studio中,如果這很重要),我正在做一個小項目,只是爲了好玩。我想創建自己的國際象棋應用程序,到目前爲止,我已經完成了很多事情。我設置了一個菜單來切換到另一個遊戲本身的活動,我用一個自繪的板子做了一個自定義視圖,並且我認爲我的模型也基本完成。唯一我不明白的是如何處理拖動。所以,當你用一個拖拽的手勢將一塊從一個位置移動到另一個位置時,你如何得到它的開始和結束點?如何處理android國際象棋應用程序中的拖動?

如上所述,我已經在我的模型中實現了一個移動(使用函數move(Position start,Position end)),並且它還檢查這個移動是否對某個片段有效,但是我仍然需要是讓我在實際電路板上拖動一塊的東西。

我在考慮在我的Controller類中放置一個onDrag方法,但我不知道如何解決這個問題,並且無法在Internet上找到好的示例。我已經開始了,但不知道它是否可以工作。

你能幫我實施拖動嗎?

在此先感謝!

P.S. 我還會在自己的問題中添加自定義視圖和(未完成)控制器的代碼,如果有幫助的話。如果你需要更多的代碼來回答這個問題,我也會把它放在這裏,讓我知道。

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

如果你正在開發遊戲的話,使用遊戲引擎,如AndEngine,libGdx等嘗試 – darwin

回答

1

希望這樣的事情會給你的想法:

@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