I know how to use dragEvent for dragging an imageView's shadow by the screen. But what I want to do is to move the imageView, changing it's coordinates. When I drag an image, it's left and top don't change (I created a timertask for printing the value regularly, and even when I'm dragging the image the coordinates don't change). It happens because I am moving the shadow, but I'd like to use a drag event for moving the imageView itself.
Methods:
private final class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
}
class MyDragListener implements View.OnDragListener {
Drawable enterShape = getResources().getDrawable(R.drawable.box);
Drawable normalShape = getResources().getDrawable(R.drawable.box);
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
System.out.println("Action is DragEvent.ACTION_DRAG_STARTED");
Log.v(LOGS, "box " + box.getX());
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
System.out.println("Action is DragEvent.ACTION_DRAG_ENTERED");
Log.v(LOGS, "box " + box.getX());
//v.setBackgroundDrawable(enterShape);
v.setBackgroundResource(R.drawable.box);
break;
case DragEvent.ACTION_DRAG_EXITED:
System.out.println("Action is DragEvent.ACTION_DRAG_FINISHED");
Log.v(LOGS, "box " + box.getX());
//v.setBackgroundDrawable(normalShape);
v.setBackgroundResource(R.drawable.box);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
//v.setBackgroundDrawable(normalShape);
default:
break;
}
return true;
}
}