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

Zig*_*iga 5 android android-animation translate-animation

我有多个可选择的视图。当其中一个被选中时,我希望我的新视图首先定位到与所选视图相同的位置,并将其移动到我的屏幕顶部。我怎样才能做到这一点?所选项目的位置各不相同。

// 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);
Run Code Online (Sandbox Code Playgroud)

A H*_*ard 6

使用ViewPropertyAnimator很简单:

viewToMove.animate().translationX(...).translationY(0).setDuration(1000); 
Run Code Online (Sandbox Code Playgroud)

TranslationY(0) 是Y 轴的顶部,在translationX(...) 处填写您希望View 在X 轴上移动到的坐标。