如何在不停止运行动画的情况下在视图上启动其他动画?

时间:2016-07-26 09:49:48

标签: android xml animation imageview

我希望ImageView一直在旋转,并在用户点击它时使其反弹。
我有两个动画,但我无法在不停止旋转动画的情况下启动弹跳动画。

我不想开始both animations at once

这是what I have AnimationSet似乎不是我需要的,因为第二个动画必须在点击时启动,而第一个动画正在运行

有人知道怎么做吗?

1 个答案:

答案 0 :(得分: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);
}