问题
I am using drag and drop functionality in my application. It works fine. Now i am facing issue while animating view when view comes back to it's original position if it is not placed on dropped view.
I have created my shadow view by
View.DragShadowBuilder shadowBuilder = new MyDragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
And my shadowbuilder class is,
private static class MyDragShadowBuilder extends View.DragShadowBuilder {
private static Drawable shadow;
public MyDragShadowBuilder(View v) {
// Stores the View parameter passed to myDragShadowBuilder.
super(v);
v.buildDrawingCache();
shadow = new BitmapDrawable(v.getDrawingCache());
}
@Override
public void onProvideShadowMetrics (Point size, Point touch){
// Defines local variables
int width, height;
width = (int)(getView().getWidth() * 1.1);
height =(int)(getView().getHeight() * 1.1);
shadow.setBounds(0, 0, width, height);
size.set(width, height);
touch.set(width / 2, height / 2);
}
@Override
public void onDrawShadow(Canvas canvas) {
// Draws the ColorDrawable in the Canvas passed in from the system.
shadow.draw(canvas);
}
}
Now, when i found unsuccessful drop, then i want my view to get animated and go back to it's original position.
case DragEvent.ACTION_DRAG_ENDED:
if (!event.getResult()) {
if (dragged.getTag() == v.getTag()){
int[] location={0,0};
v.getLocationOnScreen(location);
Log.v("values::", "x:" + x + " - y:" +x);
addViewRemoveViewFromWindow(dragged, location);
}
}
break;
I am able to get my view's original position in location array i.e. my end position. And i want my that dragged view(shadow view) position on screen. But i am not able to get it in any cases. Have tried to get it on DragEvent.ACTION_DRAG_LOCATION: case but with no luck. It always returns position of either original view's position or dropped view position. I want dragged shadow images's location. I want to apply translation animation in window between two locations. That animating view in window between 2 locations works fine when i tried by using 2 static locations.
回答1:
To animate your shadow to initial view you can use this snippet.
private void animateDragToStart(View initialView, float fromX, float fromY) {
float topMargin = fromY - initialView.getTop();
float leftMargin = fromX - initialView.getLeft();
Animation translateAnimation = new TranslateAnimation(leftMargin - (initialView.getWidth() / 2), 0, topMargin - (initialView.getHeight() / 2), 0);
translateAnimation.setDuration(500);
translateAnimation.setInterpolator(new AccelerateInterpolator());
initialView.startAnimation(translateAnimation);
initialView.setVisibility(View.VISIBLE);
}
In my case I called method from the DROP case
case DragEvent.ACTION_DROP:
View initialView = (View) event.getLocalState();//get your initial view
initialView.setVisibility(View.VISIBLE);
animateDragToStart(initialView, event.getX(), event.getY());
// Returns true. DragEvent.getResult() will return true.
return true;
And when the shadow is created you should hide the original view.
View.DragShadowBuilder shadowBuilder = new MyDragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
The question is old, but I answered because could help to somebody.
来源:https://stackoverflow.com/questions/34760518/get-dragged-shadow-location-on-view-ondraglistener