如何链接rotationAnimation?

时间:2019-07-25 09:16:16

标签: java android animation

我想旋转一个ImageView。但是我想从0旋转到45到-45,然后返回0以概括我的目标。

我尝试创建4个rotationAnimation并将其与AnimatorSet链接。

这是我的代码:

    rotate = new RotateAnimation(0, 45, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(1000);
    rotate.setInterpolator(new LinearInterpolator());
    rotate1 = new RotateAnimation(45, -45, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate1.setDuration(1000);
    rotate1.setInterpolator(new LinearInterpolator());
    rotate2 = new RotateAnimation(-45, 45, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate2.setDuration(1000);
    rotate2.setInterpolator(new LinearInterpolator());
    rotate3 = new RotateAnimation(45, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotate3.setDuration(1000);
    rotate3.setInterpolator(new LinearInterpolator());
    setAnim= new AnimationSet(true);
    setAnim.addAnimation(rotate);
    setAnim.addAnimation(rotate1);
    setAnim.addAnimation(rotate2);
    setAnim.addAnimation(rotate3);
img.startAnimation(setAnim);

此刻图像突然旋转而没有旋转到45°并返回到0°。

2 个答案:

答案 0 :(得分:0)

您可以使用AnimationListener

示例

yourFirstAnim.setAnimationListener(new AnimationListener() {

    @Override
    public void onAnimationStart(Animation animation) {
    }

    @Override
    public void onAnimationRepeat(Animation animation) {
    }

    @Override
    public void onAnimationEnd(Animation animation) {
      doYourNextAnim()
    }
  });

或更简单的方法,使用AnimatorSet()

AnimatorSet as = new AnimatorSet();
as.playSequentially(yourFirstAnim, 
                    yourSecondAnim,
                    ..,
                    ...);
as.setDuration(YOUR_DURATION);
as.start();

答案 1 :(得分:0)

您正在使用AnimationSet()正确创建链。

问题可能出在fillAfter参数上。

setFillAfter(true)动画上使用rotate,因此动画后将应用动画参数。