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()在这里不起作用。

4

4 回答 4

6

如果您使用的是 Nineoldandroids,则有一个名为reverse. 您可以将持续时间设置为 0 并调用reverse以反转动画。

于 2013-12-11T07:38:00.097 回答
5

您必须将View背面的属性设置为其原始值。

例如,如果您向前平移 40 像素,则需要向后平移 40 像素。这是因为在属性动画期间 的实际属性发生了View变化,而不仅仅是渲染方式。

http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view

于 2012-07-26T13:53:09.613 回答
4

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

android:repeatMode="reverse"
于 2017-06-07T11:33:15.390 回答
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...
于 2017-04-17T04:18:59.680 回答