6

I have multiple views that are all selectable. When one of them gets selected I want my new view to be first positioned to the same location as the selected one and also move it to the top of my screen. How can I do that? The position of selected item varies.

// Selected item
int[] location = new int[2];
item.getLocationOnScreen(location);
int positionY = location[1];

// Move the view I want to move to the position of selected item
viewToMove.setTranslationY(positionY);

// Create and start animation
Animation slideUp = new TranslateAnimation(viewToMove.getTranslationX(), viewToMove.getTranslationX(), positionY, -positionY);
slideUp.setDuration(1000);
slideUp.setFillEnabled(true);
slideUp.setFillAfter(true);
viewToMove.startAnimation(slideUp);
zbx
  • 279
  • 1
  • 3
  • 11

1 Answers1

6

With ViewPropertyAnimator its easy :

viewToMove.animate().translationX(...).translationY(0).setDuration(1000); 

translationY(0) is the top of the Y axis, and at translationX(...) you fill in the coordinates where you want the View to move to on the X axis.

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38