将动画从位置Y移动到屏幕顶部

6
我有多个可选视图。当其中一个被选中时,我希望我的新视图首先定位到与所选视图相同的位置,并将其移动到屏幕顶部。如何实现?所选项目的位置是不确定的。
// 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);
1个回答

6
使用ViewPropertyAnimator非常容易:
viewToMove.animate().translationX(...).translationY(0).setDuration(1000); 

translationY(0)是Y轴的顶部,而在translationX(...)中,您需要填写您希望View在X轴上移动到的坐标。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接