2016-07-26 47 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); 
} 
相关问题