使用AnimatorSet实现的动画回滚

17

我正在使用AnimatorSet创建动画,当它结束时,我希望将View保留在原地。

代码大致如下:

mLastAnimation = new AnimatorSet();
mLastAnimation.playTogether(
  ObjectAnimator.ofFloat(mImageView, "scaleX", 1.5f, 1f),
  ObjectAnimator.ofFloat(mImageView, "translationY", 40f, 0f));
mLastAnimation.setDuration(3000);
mLastAnimation.addListener(this);
mLastAnimation.start();

// The Activity implements the AnimatorListener interface
  @Override
  public void onAnimationEnd(Animator animator) {
    // Undo the animation changes to the view.
  }

编辑:

我正在使用新的动画 API,因此在此处无法使用 setFillAfter()


你想要倒放动画吗? - Ron
@userSeven7s:不,只是在动画后将“View”放置到原始位置。 - Macarse
4个回答

6
如果您正在使用nineoldandroids,则可以使用名为reverse的函数。您可以将持续时间设置为0并调用reverse以反转动画。

5

2
我知道这一点,但我想避免在执行动画后设置所有内容。想象一个更复杂的AnimatorSet,有更多的ObjectAnimator。将所有东西都设置回原位非常麻烦。我希望能找到一种读取AnimationSet内部内容并还原它们的方法。 - Macarse
然后尝试使用视图动画而不是属性动画。据我所知,没有其他方法可以撤消更改。 - Che Jami
或者,您可以创建“View”的快照,并在之后设置绝对值。 - Che Jami
由于我正在为旧平台使用nineOldDroids,我不确定是否可以创建View状态的快照。我会去查一下。 - Macarse

4

当使用 ObjectAnimator 时,您可以简单地设置

android:repeatMode="reverse"

3
您可以使用以下内容:
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 1.5f, 1f);
// here is the important part!
scaleX.setRepeatMode(ValueAnimator.REVERSE);
scaleX.setRepeatCount(1);
ObjectAnimator transl = ObjectAnimator.ofFloat(mImageView, "translationY", 40f, 0f));
// here is the important part!
transl.setRepeatMode(ValueAnimator.REVERSE);
transl.setRepeatCount(1);

mLastAnimation = new AnimatorSet();
mLastAnimation.playTogether(scaleX, transl);
// the rest is the same...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接