android - Android 中的旋转动画序列

标签 android animation rotation android-animation rotateanimation

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

<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" />

这是我在 Activity 类中的 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秒)?

最佳答案

根据我的经验,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);

关于android - Android 中的旋转动画序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954563/

相关文章:

c# - 可以通过在WPF中平滑地改变字体粗细来脉冲文本吗?

python - Python 版本 opencv 中的 Rodrigues 函数不起作用

javascript - 拖拽旋转界面 : how to make it work on a touchscreen?

javascript - 使用 PhoneGap 获取设备制造

WPF进度条继续动画

用于将长 URL 滑入 View 的 jQuery mousemove 动画

cocoa - NSImageViews 中的图像随机颠倒

java - 在某些设备上抛出 java.lang.UnsupportedOperationException

android - android数据绑定(bind)的优缺点是什么?

android - 为什么在单独的线程上完成工作时 UI 线程会被阻塞?