Create fade Out Animation - Android android.view.animation

Android examples for android.view.animation:Fade Animation

Description

Create fade Out Animation

Demo Code

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;

public class Main {

  private static Handler handler = new Handler() {
    @Override//from w ww  .  j a v a  2  s.  c  o m
    public void handleMessage(android.os.Message msg) {
      View view = (View) msg.obj;
      ViewGroup parent = (ViewGroup) view.getParent();
      parent.removeView(view);
    };
  };

  public static void fadeOut(final View view, long duration) {
    AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
    alphaAnimation.setDuration(duration);
    alphaAnimation.setFillAfter(true);

    alphaAnimation.setAnimationListener(new AnimationListener() {

      @Override
      public void onAnimationStart(Animation animation) {
      }

      @Override
      public void onAnimationRepeat(Animation animation) {
      }

      @Override
      public void onAnimationEnd(Animation animation) {
        Message message = Message.obtain();
        message.obj = view;
        handler.sendMessage(message);

      }
    });

    view.startAnimation(alphaAnimation);
  }

}

Related Tutorials