2016-09-14 135 views
3

我有多個可選擇的視圖。當他們中的一個被選中時,我希望我的新視圖首先定位到與所選位置相同的位置,並將其移動到屏幕頂部。我怎樣才能做到這一點?所選項目的位置不同。將動畫從位置Y翻譯到屏幕頂部

// 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); 

回答

0

隨着ViewPropertyAnimator它很容易:

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

translationY(0)爲Y軸的,你在你想要查看座標補上,並在 translationX(...)移動到X軸上。