Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translate animation from position Y to top of the screen

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);
like image 278
Ziga Avatar asked Sep 14 '16 14:09

Ziga


1 Answers

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.

like image 181
A Honey Bustard Avatar answered Sep 27 '22 23:09

A Honey Bustard