Android RotateAnimation работает не так, как ожидалось - PullRequest
0 голосов
/ 27 марта 2019

Я хочу анимировать 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 голосов
/ 27 марта 2019

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());

Остальные шаги выполняются просто, следуя приведенной выше логике.

0 голосов
/ 27 марта 2019

Причина, по которой ваше представление возвращается к нулю, состоит в том, что свойство поворота представления не изменяется.Вы должны установить поворот на представление после окончания первой анимации или использовать ObjectAnimator.ofFloat(imageview ,"rotation", 0f, 360f);, который по умолчанию устанавливает поворот представления.

...