title: Is Fade In/Out Animation Broken?
body: <p>I am getting some very weird behaviors with Android Animations and I want to understand what's going on.</p>
  
  <p>My <code>ImageView</code> is invisible at xml and I am not setting the <code>alpha</code>.</p>
  
  <h3>This code was supposed to Fade In/Out makes my <code>ImageView</code> but it doesn't animate anything and goes straight to the end effect:</h3>
  
  <pre><code>if (isFadeIn) {
      myImage.animate().alpha(1.0f).setDuration(5000).withStartAction(() -&gt; {
          myImage.setVisibility(View.VISIBLE);
          myImage.setAlpha(0.0f);
      });
  } else {
      myImage.animate().alpha(0.0f).setDuration(5000)
        .withEndAction(() -&gt; myImage.setVisibility(View.INVISIBLE));
  }
  </code></pre>
  
  <h3>Now this code Fades In and Out.....BUT the first time it runs it doesn't Fade In, and Fade Out always blinks:</h3>
  
  <pre><code>if (isFadeIn) {
      myImage.animate().alpha(1.0f).setDuration(5000).withStartAction(() -&gt; {
          myImage.setVisibility(View.VISIBLE);
          myImage.setAlpha(0.0f);
      }).withEndAction(() -&gt; myImage.setAlpha(1.0f));
  } else {
      myImage.animate().alpha(0.0f).setDuration(5000)
              .withStartAction(() -&gt; {
                  myImage.setAlpha(1.0f);
                  myImage.setVisibility(View.VISIBLE);
              })
        .withEndAction(() -&gt; myImage.setVisibility(View.INVISIBLE));
  }
  </code></pre>
  
  <p>I am cracking my head for hours trying to understand why those behaviors are arising. </p>
  
  <h3>Now, This code works:</h3>
  
  <pre><code>fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) {
          myImage.setVisibility(View.VISIBLE);
      }
      @Override public void onAnimationEnd(Animation animation) { }
      @Override public void onAnimationRepeat(Animation animation) { }
  });
  
  fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) { }
      @Override public void onAnimationEnd(Animation animation) {
          myImage.setVisibility(View.INVISIBLE);
      }
      @Override public void onAnimationRepeat(Animation animation) { }
  });
  
  fadeInAnimation.setDuration(5000);
  fadeOutAnimation.setDuration(5000);
  
  ...
  
  if (isFadeIn) {
      myImage.startAnimation(fadeInAnimation);
  } else {                         
      myImage.startAnimation(fadeOutAnimation);
  }
  </code></pre>
  
  <p>But it's too verbose, I don't like it, but for now I am sticking with it, I just wanted to understand what's wrong with the other code.</p>