I use the Android Drag & Drop API and try to set the shadow tag to the point at which the touch was made in the View . The dafault behavior should contain an anchor in the middle of the View .
I did some research, and it seems like you can do this by overriding the onProvideShadowMetrics (Point shadowSize, Point shadowTouchPoint) in the DragShadowBuilder class. From what I understood, if I changed the x, y shadowTouchPoint , it should change the binding snap coordinates.
What I did was extend the DragShadowBuilder class as follows:
class EventDragShadowBuilder extends DragShadowBuilder { int touchPointXCoord, touchPointYCoord; public EventDragShadowBuilder() { super(); } public EventDragShadowBuilder(View view, int touchPointXCoord, int touchPointYCoord) { super(view); this.touchPointXCoord = touchPointXCoord; this.touchPointYCoord = touchPointYCoord; } @Override public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) { shadowTouchPoint.set(touchPointXCoord, touchPointYCoord); super.onProvideShadowMetrics(shadowSize, shadowTouchPoint); } }
In Fragment , where I use drag & drop, I created two listeners to fire a drag event for the View :
mEventLongClickListener = new OnLongClickListener() { @Override public boolean onLongClick(View view) { EventDragShadowBuilder shadowBuilder = new EventDragShadowBuilder( view, mEventTouchXCoord, mEventTouchYCoord); view.startDrag(null, shadowBuilder, view, 0); return true; } }; // We need this listener in order to get the corect coordinates for the // drag shadow mEventTouchListener = new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { final int action = event.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { mEventTouchXCoord = (int) event.getX(); mEventTouchYCoord = (int) event.getY(); break; } } return false; } };
And I installed the towers:
itemView.setOnLongClickListener(mEventLongClickListener); itemView.setOnTouchListener(mEventTouchListener);
Everything is fine. But when I test the application and start the drag and drop process, the drop shadow is centered under the touch point. Therefore, it uses the default behavior. I tried debugging and I see that mEventTouchXCoord and mEventTouchYCoord set correctly. shadowTouchPoint.set(touchPointXCoord, touchPointYCoord); method shadowTouchPoint.set(touchPointXCoord, touchPointYCoord); gets the correct coordinates, but nonetheless it centers the shadow.
I do not know what I am doing wrong. Perhaps I misunderstood the API. Any help with a hint would be greatly appreciated.