- Drag and Drop [[1] developer.android.com ][Drag and Drop]
[Drag and Drop]: http://developer.android.com/guide/topics/ui/drag-drop.html "Drag and Drop"
UML Sequence
Critical Sectiion
private static class MyDragShadowBuilder extends View.DragShadowBuilder {
private static Drawable shadow;
public MyDragShadowBuilder(View v) {
super(v);
// Creates a draggable image that will fill the Canvas provided by the system.
shadow = new ColorDrawable(Color.LTGRAY);
}
@Override
public void onProvideShadowMetrics(Point size, Point touch) {
int width, height;
// Sets the width of the shadow to half the width of the original View
width = getView().getWidth() / 2;
// Sets the height of the shadow to half the height of the original View
height = getView().getHeight() / 2;
// The drag shadow is a ColorDrawable. This sets its dimensions to be the same as the
// Canvas that the system will provide. As a result, the drag shadow will fill the
// Canvas.
shadow.setBounds(0, 0, width, height);
// Sets the size parameter's width and height values. These get back to the system
// through the size parameter.
size.set(width, height);
// Sets the touch point's position to be in the middle of the drag shadow
touch.set(width / 2, height / 2);
}
@Override
public void onDrawShadow(Canvas canvas) {
shadow.draw(canvas);
}
}
imageView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// ClipData.Item item = new ClipData.Item(v.getTag().toString());
// Create a new ClipData using the tag as a label, the plain text MIME type, and
// the already-created item. This will create a new ClipDescription object within the
// ClipData, and set its MIME type entry to "text/plain"
ClipData data = ClipData.newPlainText("dot", "Dot : " + v.toString());
v.startDrag(data, new MyDragShadowBuilder(v), v, 0);
return false;
}
});
private void processDrop(DragEvent event) {
final ClipData data = event.getClipData();
final int N = data.getItemCount();
for (int i = 0; i < N; i++) {
ClipData.Item item = data.getItemAt(i);
Log.i(TAG, "Dropped item " + i + " : " + item);
String text = item.coerceToText(getContext()).toString();
if (event.getLocalState() == (Object) this) {
text += " : Dropped on self!";
}
}
}
Valid DragEvent data by action type
getAction() | getClipDescription() | getLocalState() | getX() | getY() | getClipData() | getResult() |
---|---|---|---|---|---|---|
DRAG_STARTED | √ | √ | √ | |||
DRAG_ENTERED | √ | √ | √ | √ | ||
DRAG_LOCATION | √ | √ | √ | √ | ||
DRAG_EXITED | √ | √ | ||||
ACTION_DROP | √ | √ | √ | √ | √ | |
DRAG_ENDED | √ | √ | √ |