Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Drag and Drop - Animation shadow to destination

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

like image 932
Pepa Zapletal Avatar asked Oct 12 '16 12:10

Pepa Zapletal


1 Answers

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(); } 
like image 130
Rabindra Khadka Avatar answered Oct 16 '22 11:10

Rabindra Khadka