Translate a view to the specified translation values, and animate the translations to 0. - Android Animation

Android examples for Animation:Translate Animation

Description

Translate a view to the specified translation values, and animate the translations to 0.

Demo Code


import java.util.List;

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

public class Main {
  private static Interpolator sDecelerateQuintInterpolator;
  /**//from   w  w w  .jav a  2s . com
   * 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;

  /**
   * Translate a view to the specified translation values, and animate the
   * translations to 0.
   */
  public static void addXYTranslationAnimators(List<Animator> animators, final View view, int xTranslation,
      int yTranslation, int animationDelay) {
    addXTranslationAnimators(animators, view, xTranslation, 0, animationDelay, null);
    addYTranslationAnimators(animators, view, yTranslation, 0, animationDelay, null);
  }

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

  /**
   * Add animations to translate a view's Y-translation. {@link AnimatorListener}
   * can be null.
   */
  private static void addYTranslationAnimators(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.setTranslationY(startTranslation);
    final ObjectAnimator translateAnimatorY = ObjectAnimator.ofFloat(view, View.TRANSLATION_Y, startTranslation,
        endTranslation);
    translateAnimatorY.setInterpolator(sDecelerateQuintInterpolator);
    translateAnimatorY.setDuration(sAnimationDuration);
    translateAnimatorY.setStartDelay(animationDelay);

    if (listener != null) {
      translateAnimatorY.addListener(listener);
    }

    animators.add(translateAnimatorY);
  }
}

Related Tutorials