2017-02-27 71 views
1

我試圖刪除顯示一個視圖,但看起來像旋轉卡的方式。
下面沒有旋轉視圖,但不是我想要做的。連鎖動畫的旋轉,直到視圖不在窗口

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <rotate 
     android:duration="1500" 
     android:fromDegrees="0" 
     android:pivotX="100%" 
     android:pivotY="50%" 
     android:startOffset="0" 
     android:toDegrees="220" /> 
</set> 

什麼我以後是不是旋轉本身圍繞一個固定的中心,但類似投擲卡的議案。
我該怎麼做?

更新
我想這個答案之後,從@loadedion但不工作:

ObjectAnimator rotateAnimation = ObjectAnimator.ofFloat(rootView, "rotation", 0.0f, 360f); 
rotateAnimation.setDuration(5000); 

ObjectAnimator throwAnimation = ObjectAnimator.ofFloat(rootView, "x", rootView.getX(), rootView.getX() + 200); 
throwAnimation.setInterpolator(new AccelerateInterpolator()); 
     throwAnimation.setDuration(3000); 
ObjectAnimator throwAnimation2 = ObjectAnimator.ofFloat(rootView, "y", rootView.getY(), rootView.getY() + 200);  
throwAnimation.setInterpolator(new AccelerateInterpolator()); 
throwAnimation.setDuration(3000); 
AnimatorSet cardThrowAnimations = new AnimatorSet(); 
cardThrowAnimations.playSequentially(rotateAnimation, throwAnimation, throwAnimation2); 
     cardThrowAnimations.start(); 
+0

我相信'playSequentially'會等到每個動畫在開始下一個之前完成,所以看起來像這樣會導致卡真正緩慢旋轉5秒,然後稍稍向右移動3秒,然後稍微向下移動3秒conds。那是你所看到的嗎?使用'playTogether'可以同時運行所有的動畫。 – loadedion

回答

0

如果你想有一個視圖旋轉,你必須動畫應用到它。

ObjectAnimator rotateAnimation = ObjectAnimator.ofFloat(targetView, "rotation", 0.0f, 360f); 
rotateAnimation.setRepeatCount(ObjectAnimator.INFINITE); 
rotateAnimation.setRepeatMode(ObjectAnimator.RESTART); 
rotateAnimation.setInterpolator(new LinearInterpolator()); 
rotateAnimation.setDuration(DURATION_ROTATION); 

如果你希望它滑出像扔卡,而它的旋轉,你可以創建另一個ObjectAnimator來設置它的x位置。

ObjectAnimator throwAnimation = ObjectAnimator.ofFloat(targetView, "x", targetView.getX(), targetView.getX() + 500); // move 500 pixels to the right 
throwAnimation.setInterpolator(new AccelerateInterpolator()); 
throwAnimation.setDuration(DURATION_THROW); 

,然後啓動動畫

rotateAnimation.start(); 
throwAnimation.start(); 

另外,您也可以將它們組合起來在AnimatorSet開始在一起:

AnimatorSet cardThrowAnimations = new AnimatorSet(); 
cardThrowAnimations.playTogether(rotateAnimation, throwAnimation); 
cardThrowAnimations.start(); 
+0

我在想我可能需要使用2個動畫,但我怎樣才能找出第二個動畫的X,Y位置? – Jim

+0

在第一次旋轉結束後,START_X_POSITION AND START_Y_POSITION應該由視圖的新位置確定?怎麼樣? – Jim

+0

@Jim你可以在你的佈局中將卡片視圖定位在動畫的開始位置,這樣你就可以通過'targetPosition.getX()'和'targetPosition.getY()'獲得開始的x,y(確保輪詢視圖之後的x,y位置已經創建) – loadedion