add Slide In From Right Animators - Android Animation

Android examples for Animation:Slide Animation

Description

add Slide In From Right Animators

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   w  w  w . ja  v  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;

  public static void addSlideInFromRightAnimators(List<Animator> animators, final View view, int startTranslation,
      int animationDelay) {
    addXTranslationAnimators(animators, view, startTranslation, 0, animationDelay, null);
    addFadeAnimators(animators, view, 0, 1.0f, animationDelay);
  }

  /**
   * 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 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