【问题标题】:Rotate animation sequence in Android在Android中旋转动画序列
【发布时间】:2016-08-24 10:25:07
【问题描述】:

我想在 Android 项目中制作简单的动画。我的活动中有一张图片:

<ImageView
        android:id="@+id/pointer_png"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:adjustViewBounds="true"
        android:layout_gravity="center"
        android:src="@drawable/pointer_400" />

这是我在活动类中的 onClick 方法:

public void onStartButtonClick(View view){
    AnimationSet animationSet = new AnimationSet(true);
    animationSet.setInterpolator(new LinearInterpolator());
    animationSet.setFillAfter(true);

    RotateAnimation anim = new RotateAnimation(0.0f, -45.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    anim.setDuration(4000);
    animationSet.addAnimation(anim);

    RotateAnimation anim2 = new RotateAnimation(0.0f, 90.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    anim2.setDuration(4000);
    animationSet.addAnimation(anim2);

    RotateAnimation anim3 = new RotateAnimation(0.0f, -135.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    anim3.setDuration(4000);
    animationSet.addAnimation(anim3);

    RotateAnimation anim4 = new RotateAnimation(0.0f, 180.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    anim4.setDuration(4000);
    animationSet.addAnimation(anim4);

    final ImageView pointer = (ImageView) findViewById(R.id.pointer_png);
    pointer.startAnimation(animationSet);
}

很遗憾,效果出乎意料。我想按这个顺序旋转图像:

  1. 在 4 秒内旋转 180 度。
  2. 在接下来的 4 秒内旋转 -135 度。
  3. 在接下来的 4 秒内旋转 90 度。
  4. 在最后 4 秒内旋转 -45 度。

但是用这个代码动画肯定短于 16 秒,它只由一个部分组成 - 从 0 点开始 90 度,它就结束了。 AnimationSet 可能会检查所有动画并计算我序列中的最后一个位置。我尝试设置 AnimationSet(false) 并向每个 RotateAnimation 添加单独的 LinearInterpolator,但它不起作用。

我应该怎么做才能使我的动画更长并且所有旋转都分开(4 步,每步 4 秒)?

【问题讨论】:

  • 尝试使用setStartOffset()为您的其他动画添加起始偏移量。
  • 我尝试过使用偏移量:0、4000、8000 和 12000,但最终效果非常意外且出乎意料。恐怕我需要为每个动画设置 AnimationListener 并覆盖 onAnimationEnd 方法,然后开始下一个动画。这个序列没问题,但是更复杂的动画呢?

标签: android animation rotation android-animation rotateanimation


【解决方案1】:

根据我的经验,AnimationSet 并不总是按预期工作,并且可能会让人感到痛苦。我会尝试使用ViewPropertyAnimator

这是一个如何使用它的示例。您可以像这样设置 startDelay :

pointer.animate()
    .rotation(...)   // <- enter rotation values here
    .setStartDelay(4000)
    .setInterpolator(new LinearInterpolator())
    .setDuration(4000);

或设置AnimationListener 并在前一个动画完成后在onAnimationEnd() 中启动下一个动画。

如果我不得不这样做,我会自发地编写自己的方法,类似这样(未测试):

private void rotate(View v, float rotation, int startDelay) {
    v.animate()
     .rotation(rotation)  // or rotationBy(rotation) whichever suits you better
     .setStartDelay(startDelay)
     .setInterpolator(new LinearInterpolator())
     .setDuration(4000);
}

然后像这样调用它四次:

rotate(pointer, -45, 0);
rotate(pointer, 90, 4000);
rotate(pointer, -135, 8000);
rotate(pointer, 180, 12000);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-15
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
相关资源
最近更新 更多