start Fade In Animation - Android android.view.animation

Android examples for android.view.animation:Fade Animation

Description

start Fade In Animation

Demo Code

import android.annotation.SuppressLint;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

public class Main {

  public static void startFadeInAnimation(final View view, long duration) {
    startAnimation(view, getFadeInOutAnimation(view, 0, 1, duration));

  }/*w  w  w.  j a  va  2s .  c o  m*/

  @SuppressLint("NewApi")
  public static AlphaAnimation getFadeInOutAnimation(final View view, final float fromAlpha, final float toAlpha,
      long duration) {
    AlphaAnimation fadeInOut = new AlphaAnimation(fromAlpha, toAlpha);
    fadeInOut.setAnimationListener(new Animation.AnimationListener() {
      public void onAnimationStart(Animation animation) {
        view.setAlpha(fromAlpha); // setAlpha() since from API level 11, Android 3.0
      }

      public void onAnimationEnd(Animation animation) {
        view.setAlpha(toAlpha); // setAlpha() since from API level 11, Android 3.0
      }

      public void onAnimationRepeat(Animation animation) {
      }
    });
    fadeInOut.setDuration(duration);
    return fadeInOut;
  }

  public static void startAnimation(View view, Animation anim) {
    view.clearAnimation();
    view.startAnimation(anim);
    // view.invalidate();
  }

}

Related Tutorials