Android RotateAnimation无法正常工作

时间:2019-03-27 15:55:17

标签: java android animation android-animation

我想以这种方式对imageView进行动画处理:(按三角学定义的角度符号)

  1. 从0到-45平滑旋转
  2. 从-45平滑旋转到+45
  3. 从+45平滑旋转到-45
  4. 重新启动为2(无限循环)

使用下面的代码,我得到:

  1. 从0到+45平滑旋转
  2. 立即恢复到0
  3. 从0到-45平滑旋转
  4. 从-45平滑旋转到0
  5. 重新开始3(无限循环)

所以,根本不是我想要的!

有人看到如何解决这个问题?

谢谢!

这是我的代码:

float angle = 45f;

        RotateAnimation rotateAnimation1 = new RotateAnimation(0, -angle,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);            
        rotateAnimation1.setStartOffset(0);
        rotateAnimation1.setDuration(2000);
        rotateAnimation1.setInterpolator(new LinearInterpolator());



        RotateAnimation rotateAnimation2 = new RotateAnimation(-angle, angle,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //  animationSet.addAnimation(rotateAnimation);
        rotateAnimation2.setStartOffset(0);
        rotateAnimation2.setDuration(4000);
        rotateAnimation2.setInterpolator(new LinearInterpolator());



        RotateAnimation rotateAnimation3 = new RotateAnimation(angle, -angle,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);       
        rotateAnimation3.setStartOffset(4000);
        rotateAnimation3.setDuration(4000);
        rotateAnimation3.setInterpolator(new LinearInterpolator());



        final AnimationSet animSet1 = new AnimationSet(true);
        animSet1.setFillEnabled(true);
        animSet1.addAnimation(rotateAnimation1);


        final AnimationSet animSet2 = new AnimationSet(true);
       // animSet2.setFillEnabled(true);
        animSet2.addAnimation(rotateAnimation2);
        animSet2.addAnimation(rotateAnimation3);


        animSet1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                view.startAnimation(animSet2);


            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });


        animSet2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                view.startAnimation(animSet2);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });





        view.startAnimation(animSet1);

2 个答案:

答案 0 :(得分:0)

视图返回零的原因是视图的旋转属性未更改。您应该在第一个动画结束后将旋转设置为打开视图,或使用ObjectAnimator.ofFloat(imageview ,"rotation", 0f, 360f);来默认设置视图的旋转。

答案 1 :(得分:0)

1。从0到-45平滑旋转:反转角度符号和setFillAfter(true),以便视图保持动画位置结束

    RotateAnimation rotateAnimation1 = new RotateAnimation(0, 45f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation1.setFillAfter(true); //This keeps view at animation ended position            
    rotateAnimation1.setStartOffset(0);
    rotateAnimation1.setDuration(2000);
    rotateAnimation1.setInterpolator(new LinearInterpolator());

2。从-45平滑旋转到+45::将动画的起始角度设置为0,因为视图已经旋转。

    // Since view already is at -45 position due to rotateAnimation1.setFillAfter(true), now start point is 0 again
    RotateAnimation rotateAnimation2 = new RotateAnimation(0, -90,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation2.setFillAfter(true);
    rotateAnimation2.setStartOffset(0);
    rotateAnimation2.setDuration(4000);
    rotateAnimation2.setInterpolator(new LinearInterpolator());

按照上面的逻辑,其余步骤很简单。

相关问题