2016-07-26 88 views
0

我想讓ImageView始終在旋轉,並在用戶點擊時使其反彈。
我有兩個動畫,但無法停止旋轉動畫而無法啓動彈跳動畫。

我不想開始both animations at once

這是what I have
AnimationSet似乎並不是我所需要的,因爲第二個動畫必須先點擊,而第一個正在運行
如何在不停止運行動畫的情況下在視圖上啓動其他動畫?

有沒有人知道如何做到這一點?

+0

使用兩個'Animator's,而不是兩個'Animation's – pskink

回答

0

謝謝pskink,it works fine! 以下是工作代碼:

private ImageView rotating_image; 
private AnimatorSet bounceAnimatorSet; 
private ObjectAnimator rotationAnimator; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ... 
    rotating_image = (ImageView) findViewById(R.id.rotating_image); 
    if (rotating_image != null) 
     rotating_image.setOnClickListener(this); 
    setRotation(); 
    rotationAnimator.start(); 
    setBounceAnimators(); 
    ... 
} 

private void setRotation(){ 
    rotationAnimator = ObjectAnimator.ofFloat(rotating_image, "rotation",0,360); 
    rotationAnimator.setDuration(4000); 
    rotationAnimator.setRepeatCount(ValueAnimator.INFINITE); 
    rotationAnimator.setRepeatMode(ValueAnimator.RESTART); 
    rotationAnimator.setInterpolator(new LinearInterpolator()); 
} 


private void setBounceAnimators(){ 
    bounceAnimatorSet = new AnimatorSet(); 
    ObjectAnimator enlargeX = ObjectAnimator.ofFloat(rotating_image, "scaleX",1,1.5f); 
    enlargeX.setDuration(800); 
    enlargeX.setInterpolator(new LinearInterpolator()); 

    ObjectAnimator enlargeY = ObjectAnimator.ofFloat(rotating_image, "scaleY",1,1.5f); 
    enlargeY.setDuration(800); 
    enlargeY.setInterpolator(new LinearInterpolator()); 

    ObjectAnimator bounceX = ObjectAnimator.ofFloat(rotating_image, "scaleX", 1.5f, 1); 
    bounceX.setDuration(1000); 
    bounceX.setInterpolator(new BounceInterpolator()); 

    ObjectAnimator bounceY = ObjectAnimator.ofFloat(rotating_image, "scaleY", 1.5f, 1); 
    bounceY.setDuration(1000); 
    bounceY.setInterpolator(new BounceInterpolator()); 

    bounceAnimatorSet.play(enlargeX).with(enlargeY); 
    bounceAnimatorSet.play(bounceY).with(bounceX).after(enlargeY); 
} 
相關問題