Android - Drag and Drop - Animation shadow to destination Android - Drag and Drop - Animation shadow to destination android android

Android - Drag and Drop - Animation shadow to destination


First implement onTouchlistener on the view

llDragable.setOnTouchListener(this);

Make a view draggable

@Overridepublic 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();}


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.


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!