Slide a view out of view from the start to end position, fading the view out as it approaches the end position. - Android Animation

Android examples for Animation:Slide Animation

Description

Slide a view out of view from the start to end position, fading the view out as it approaches the end position.

Demo Code


import java.util.List;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
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  www  .  ja v  a  2s .c  om*/
   * 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;

  /**
   * Slide a view out of view from the start to end position, fading the view out
   * as it approaches the end position.
   */
  public static void addSlideOutAnimators(List<Animator> animators, final View view, int startTranslation,
      int endTranslation, int animationDelay) {
    addFadeAnimators(animators, view, view.getAlpha(), 0, animationDelay);
    addXTranslationAnimators(animators, view, startTranslation, endTranslation, animationDelay, null);
  }

  /**
   * Slide a view out of view from the start to end position, fading the view out
   * as it approaches the end position. The animators are expected to run
   * immediately without a start delay.
   */
  public static void addSlideOutAnimators(List<Animator> animators, final View view, int startTranslation,
      int endTranslation) {
    addSlideOutAnimators(animators, view, startTranslation, endTranslation, 0 /* animation delay */);
  }

  /**
   * 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 */);
  }

  /**
   * Add animations to translate a view's X-translation. {@link AnimatorListener}
   * can be null.
   */
  private static void addXTranslationAnimators(List<Animator> animators, final View view, int startTranslation,
      final int endTranslation, int animationDelay, AnimatorListener listener) {
    // We used to skip the animation if startTranslation == endTranslation,
    // but to add a recycle view listener, we need at least one animation
    view.setTranslationX(startTranslation);
    final ObjectAnimator translateAnimatorX = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, startTranslation,
        endTranslation);
    translateAnimatorX.setInterpolator(sDecelerateQuintInterpolator);
    translateAnimatorX.setDuration(sAnimationDuration);
    translateAnimatorX.setStartDelay(animationDelay);
    if (listener != null) {
      translateAnimatorX.addListener(listener);
    }

    animators.add(translateAnimatorX);
  }
}

Related Tutorials