My Drag and Drop Goals
I had 3 specific goals when it came to implementing drag and drop with regards to moving the chess piece on the board.
Changing the ImageView on Drag Start
As I began working, I realized that the drag drop functionality did not support what I wanted right out of the box, but certainly with a little work there was enough in the api to give me what I was looking for. When the user presses down on the chess piece (which is an ImageView) to start a drag, here are the steps I take:
Changing the chess piece image on drag start achieved!
So far i covered how to hide an ImageView and just showed the DragShadowBuilder object at the start of a drag. Now i'm going to concentrate on restricting the drag area.
Trying to Restrict the Drag Area
Since my application involves moving a chess piece around board, I wanted to restrict the dragging area to the game board itself. My motivation is that the ImageView is hidden at the start of the drag. If the ImageView is dropped off the board, it will never reappear. That would leave my application in an inconsistent state. So I spent some time looking through the Android api, and Googling how I might limit the drag area. I came up with nothing. Then I took a step back and thought what problem am I trying to solve? Is it the user dragging the ImageView off the board? No, the real problem is the drop action is never handled, so I never get a chance to make the ImageView visible again. So I redefined my problem to be determining when a drop is valid or not.
Determining Valid Drops
To figure out how to determine valid drops, I went back to the Android Developer documentation. I honed in on handling drag end events. Here are the key points I found:
Now users can drag and drop the ImageView anywhere and the game will not end up in an inconsistent state. My second goal is achieved.
The final installment covers moving a game piece as a result of a valid move or having that game piece “snap back” to it’s original postion when an invalid move is made.
Application Background Information
Some background information on my application might be helpful for this post. The game involves moving a single chess piece (a knight) around a chessboard. The chessboard is a TableLayout and each square is a subclass of LinearLayout and has an OnDragListener set. Additionally, each OnDragListener has a reference to a “mediator” object that:
When the user starts to drag the ImageView and passes over a given square, the following actions are possible:
As you can see from above, regardless if the move is valid or not, the ImageView is made visible. By making the ImageView visible immediately after the drop, my goals of a seamless move, or “snapping back” to it’s original postion are met.
Moving the ImageView
Earlier, I mentioned that each square was a subclass of LayoutView. This was done for the ease of moving the ImageView from square to square. Here are the details from the checkForValidMove method (line 14 in the code sample above) that show how the move of the ImageView is accomplished.
I hope that the reader will find this article helpful in their own Android development efforts.
I had 3 specific goals when it came to implementing drag and drop with regards to moving the chess piece on the board.
- When a user presses down and starts to drag, the chess piece would be replaced by a much lighter version of the original, making it obvious that it is being moved.
- I wanted to limit the area where the chess piece could be dragged, more specifically I don’t want players to drag the piece off the board
- If the player tries to drop the game piece that would result in an illegal move, I wanted the game piece to “snap back” to it’s original position.
Changing the ImageView on Drag Start
As I began working, I realized that the drag drop functionality did not support what I wanted right out of the box, but certainly with a little work there was enough in the api to give me what I was looking for. When the user presses down on the chess piece (which is an ImageView) to start a drag, here are the steps I take:
- Create a DragShadowBuilder object.
- Call the startDrag method.
- Hide the original ImageView (the chess piece) by calling setVisibility and passing in View.INVISIBLE, leaving only the DragShadowBuilder object visible, clearly indicating the drag has started.
@Override |
public boolean onTouch(View view, MotionEvent motionEvent) { |
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { |
ClipData clipData = ClipData.newPlainText("", ""); |
View.DragShadowBuilder dsb = new View.DragShadowBuilder(view); |
view.startDrag(clipData, dsb, view, 0); |
view.setVisibility(View.INVISIBLE); |
return true; |
} else { |
return false; |
} |
} |
Changing the chess piece image on drag start achieved!
So far i covered how to hide an ImageView and just showed the DragShadowBuilder object at the start of a drag. Now i'm going to concentrate on restricting the drag area.
Trying to Restrict the Drag Area
Since my application involves moving a chess piece around board, I wanted to restrict the dragging area to the game board itself. My motivation is that the ImageView is hidden at the start of the drag. If the ImageView is dropped off the board, it will never reappear. That would leave my application in an inconsistent state. So I spent some time looking through the Android api, and Googling how I might limit the drag area. I came up with nothing. Then I took a step back and thought what problem am I trying to solve? Is it the user dragging the ImageView off the board? No, the real problem is the drop action is never handled, so I never get a chance to make the ImageView visible again. So I redefined my problem to be determining when a drop is valid or not.
Determining Valid Drops
To figure out how to determine valid drops, I went back to the Android Developer documentation. I honed in on handling drag end events. Here are the key points I found:
- When a drag ends, an ACTION_DRAG_ENDED event is sent to all DragListeners.
- A DragListener can determine more information about drag operations by calling the getResult method of the DragEvent.
- If a DragListener returned true from an ACTION_DROP event, the getResult call will return true, otherwise false.
- When an ACTION_DRAG_ENDED event happens, call the getResult method
- If getResult returns false, immediately set the visibility of the ImageView (used at the start of the drag) back to visible.
@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_DRAG_ENDED) { |
if (dropEventNotHandled(dragEvent)) { |
dragView.setVisibility(View.VISIBLE); |
} |
} else if (dragAction == DragEvent.ACTION_DROP && containsDragable) { |
checkForValidMove((ChessBoardSquareLayoutView) view, dragView); |
dragView.setVisibility(View.VISIBLE); |
} |
return true; |
} |
private boolean dropEventNotHandled(DragEvent dragEvent) { |
return !dragEvent.getResult(); |
} |
Now users can drag and drop the ImageView anywhere and the game will not end up in an inconsistent state. My second goal is achieved.
The final installment covers moving a game piece as a result of a valid move or having that game piece “snap back” to it’s original postion when an invalid move is made.
Application Background Information
Some background information on my application might be helpful for this post. The game involves moving a single chess piece (a knight) around a chessboard. The chessboard is a TableLayout and each square is a subclass of LinearLayout and has an OnDragListener set. Additionally, each OnDragListener has a reference to a “mediator” object that:
- Handles communication between objects.
- Keeps track of the current square (landing square of the last valid move).
When the user starts to drag the ImageView and passes over a given square, the following actions are possible:
- Use the ACTION_DRAG_ENTERED event to set the instance variable ‘containsDraggable’ to true.
- Use the ACTION_DRAG_EXITED event to set ‘containsDraggable’ to false.
- When an ACTION_DROP event occurs on a square, the mediator is asked if the attempted move is valid by checking all possible valid moves (pre-calculated) from the “current” square.
@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_DRAG_ENDED) { |
if (dropEventNotHandled(dragEvent)) { |
dragView.setVisibility(View.VISIBLE); |
} |
} else if (dragAction == DragEvent.ACTION_DROP && containsDragable) { |
checkForValidMove((ChessBoardSquareLayoutView) view, dragView); |
dragView.setVisibility(View.VISIBLE); |
} |
return true; |
} |
As you can see from above, regardless if the move is valid or not, the ImageView is made visible. By making the ImageView visible immediately after the drop, my goals of a seamless move, or “snapping back” to it’s original postion are met.
Moving the ImageView
Earlier, I mentioned that each square was a subclass of LayoutView. This was done for the ease of moving the ImageView from square to square. Here are the details from the checkForValidMove method (line 14 in the code sample above) that show how the move of the ImageView is accomplished.
private void checkForValidMove(ChessBoardSquareLayoutView view, View dragView) { |
if (mediator.isValidMove(view)) { |
ViewGroup owner = (ViewGroup) dragView.getParent(); |
owner.removeView(dragView); |
view.addView(dragView); |
view.setGravity(Gravity.CENTER); |
view.showAsLanded(); |
mediator.handleMove(view); |
} |
} |
I hope that the reader will find this article helpful in their own Android development efforts.
where is size control on drag if you have any idea about this please mail me on md.hussain.jh@gmail.com
ReplyDelete