【问题标题】:How to start an additional animation on a View without stopping its running animation?如何在视图上启动附加动画而不停止其正在运行的动画?
【发布时间】:2016-07-26 09:49:47
【问题描述】:

我希望 ImageView 一直在旋转,并在用户单击它时使其弹跳。
我有这两种动画,但如果不停止旋转动画,我就无法开始弹跳动画。

我不想开始 both animations at once

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

有人知道怎么做吗?

【问题讨论】:

  • 使用两个Animators,而不是两个Animations

标签: android xml animation imageview


【解决方案1】:

谢谢你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);
}

【讨论】:

    猜你喜欢
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多