Disabling animations on a device crashes the app

Viewed 1007

I have an AnimationDrawable, which plays a frame animation and then the next Activity is started with a circular reveal animation:

  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
          AnimationDrawable animationDrawable =
                  (AnimationDrawable) mAnimationSceneImage.getDrawable();
          animationDrawable.start();

          mHandler.postDelayed(new Runnable() {
              @Override
              public void run() {
                  startMapActivityWithAnimation();
              }
          }, 1000);
      }
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  private void startMapActivityWithAnimation() {
      int colorFrom = ContextCompat.getColor(this, R.color.window_background_splash);
      int colorTo = ContextCompat.getColor(this, android.R.color.white);

      ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo);
      colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator valueAnimator) {
              mCircularRevealView.setBackgroundColor((Integer) valueAnimator.getAnimatedValue());
          }
      });

      int cx = mCircularRevealView.getWidth() / 2;
      int cy = mCircularRevealView.getHeight() / 2;
      float finalRadius = Math.max(cx, cy);

      Animator circularRevealAnimation = ViewAnimationUtils
              .createCircularReveal(mCircularRevealView, cx, cy, 0, finalRadius);
      mCircularRevealView.setVisibility(View.VISIBLE);

      AnimatorSet animatorSet = new AnimatorSet();
      animatorSet.setDuration(200);
      animatorSet.addListener(new AnimatorListenerAdapter() {
          @Override
          public void onAnimationStart(Animator animation) {
              mLogoImage.animate()
                      .alpha(0.0f)
                      .setDuration(200)
                      .setInterpolator(new AccelerateInterpolator())
                      .start();
            }

          @Override
          public void onAnimationEnd(Animator animation) {
              startMapActivity();
          }
      });
      animatorSet.playTogether(colorAnimation, circularRevealAnimation);
      animatorSet.start();
    }

The animations run perfectly on a normal device. I also have some Espresso UI tests, so I've disabled all the animations in the developer options screen on the test device and now I'm getting the following exception:

java.lang.IllegalStateException: Animator has already started, cannot change it now!
             at android.view.RenderNodeAnimator.checkMutable(RenderNodeAnimator.java:149)
             at android.view.RenderNodeAnimator.setDuration(RenderNodeAnimator.java:324)
             at android.view.RenderNodeAnimator.setDuration(RenderNodeAnimator.java:322)
             at android.animation.AnimatorSet.updateAnimatorsDuration(AnimatorSet.java:760)
             at android.animation.AnimatorSet.getTotalDuration(AnimatorSet.java:1721)
             at android.animation.AnimatorSet.forceToEnd(AnimatorSet.java:446)
             at android.animation.AnimatorSet.doAnimationFrame(AnimatorSet.java:981)
             at android.animation.AnimationHandler.doAnimationFrame(AnimationHandler.java:145)
             at android.animation.AnimationHandler.-wrap2(Unknown Source:0)
             at android.animation.AnimationHandler$1.doFrame(AnimationHandler.java:54)
             at android.view.Choreographer$CallbackRecord.run(Choreographer.java:884)
             at android.view.Choreographer.doCallbacks(Choreographer.java:698)
             at android.view.Choreographer.doFrame(Choreographer.java:630)
             at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:872)
             at android.os.Handler.handleCallback(Handler.java:769)
             at android.os.Handler.dispatchMessage(Handler.java:98)
             at android.os.Looper.loop(Looper.java:164)
             at android.app.ActivityThread.main(ActivityThread.java:6540)
             at java.lang.reflect.Method.invoke(Native Method)
             at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

Do you have any suggestions how to fix this problem?

3 Answers
Related