Add animations to fade a view from the specified start alpha value to end value. - Android Animation

Android examples for Animation:Fade Animation

Description

Add animations to fade a view from the specified start alpha value to end value.

Demo Code


import java.util.List;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.animation.Interpolator;

public class Main {
  private static Interpolator sDecelerateQuintInterpolator;
  /**//from  w  w w  . ja v  a2 s  . co m
   * Duration of an individual animation when the children of the grid are laid
   * out again. This is measured in milliseconds.
   */
  private static int sAnimationDuration = 450;

  /**
   * Add animations to fade a view from the specified start alpha value to end
   * value.
   */
  public static void addFadeAnimators(List<Animator> animators, final View view, float startAlpha, final float endAlpha,
      int animationDelay) {
    if (startAlpha == endAlpha) {
      return;
    }

    view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    view.setAlpha(startAlpha);

    final ObjectAnimator fadeAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, view.getAlpha(), endAlpha);
    fadeAnimator.setInterpolator(sDecelerateQuintInterpolator);
    fadeAnimator.setDuration(sAnimationDuration);
    fadeAnimator.setStartDelay(animationDelay);
    fadeAnimator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        view.setAlpha(endAlpha);
        view.setLayerType(View.LAYER_TYPE_NONE, null);
      }
    });
    animators.add(fadeAnimator);
  }

  /**
   * Add animations to fade a view from the specified start alpha value to end
   * value. The animators are expected to run immediately without a start delay.
   */
  public static void addFadeAnimators(List<Animator> animators, final View view, float startAlpha,
      final float endAlpha) {
    addFadeAnimators(animators, view, startAlpha, endAlpha, 0 /* animation delay */);
  }
}

Related Tutorials