27

I want to animate a ShadowView to some coordinates (to destination view).

I'm using D&D and if user drops (DragEvent.ACTION_DROP) then the view in some area I want to animate the view (from the drop location) to some destination view.

I don't want to animate view from the source location but want to do from the DROP location.

I tried a lot of things but nothing works. How can I get access to the ShadowView? This also not working:

EventDragShadowBuilder.getView()

I think TranslateAnimation should work for this but I need access to the "shadow" view during the D&D.

Image: enter image description here

Hash
  • 4,647
  • 5
  • 21
  • 39
Pepa Zapletal
  • 2,879
  • 3
  • 39
  • 69
  • 1
    I don't think you can get access to ShadowView. If you can get the dropped location, you could create a duplicate View yourself with same background as the ShadowView and animate this duplicate view to destination giving the illusion that ShdowView itself is moving. – Abhishek V Oct 21 '16 at 05:35

4 Answers4

1

First implement onTouchlistener on the view

llDragable.setOnTouchListener(this);

Make a view draggable

@Override
public boolean onTouch(View view, MotionEvent event) {
    float dX = 0;
    float dY = 0;
    switch (view.getId()){
        case R.id.dragableLayout :{
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    dX = view.getX() - event.getRawX();
                    dY = view.getY() - event.getRawY();
                    lastAction = MotionEvent.ACTION_DOWN;
                    break;

                case MotionEvent.ACTION_MOVE:
                    view.setY(event.getRawY() + dY);
                    view.setX(event.getRawX() + dX);
                    lastAction = MotionEvent.ACTION_MOVE;
                    break;

                case MotionEvent.ACTION_UP:
                    //Animate
                    animate();
                    if (lastAction == MotionEvent.ACTION_DOWN)
                        //Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show();
                        break;

                default:
                    return false;
            }
            return true;
        }
    }
    return false;
}

Then you can use object animator at case MotionEvent.ACTION_UP using object animation . You need to have the position of the destination.

private void animate() {
    Path path = new Path();
    path.moveTo(destinationX, destinationY);
    path.lineTo(destinationX, destinationY);
    ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mButton, View.X, View.Y, path);
    objectAnimator.setDuration(duration);
    objectAnimator.setInterpolator(new LinearInterpolator());
    objectAnimator.start();
}
Rabindra Khadka
  • 1,344
  • 1
  • 13
  • 23
0

One possible thing could be

once the drag event has finished, you can move the "view" to the location of the drop and then you can start an animation to move the view from the dropped location to the destination.

Keep in mind here the drag shadow is not being animated but the view itself is being animated.

Samarth S
  • 313
  • 4
  • 5
0

As a workaround, you can try to add a selector with transparent and black-gradient drawables, depended on appropriate state. In this case you will decide, when your "shadow" should be shown and when it should be gone.

Here is a question about "shadow" bordered layouts: Android LinearLayout : Add border with shadow around a linearLayout

Don't sure, is it a good way... But it might work. Good luck!

0

Create a duplicate view of the one you are dragging. Once drag ends, you get the location or drop off co-ordinates of the view. Once this happens, set the visibility of your "real" view to View.INVISIBLE. Then set the x and y of the temp invisible view to that of the drop-off point and make it visible. After that, create a translate animation that animates the temp view to the desired position, and don't forget to set the setFillEnabled and setFillAfter properties of the translate animation to true.