Android examples for User Interface:View Animation
Translate a view to the specified translation values, while rotating to the specified rotation value.
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 ww .j a v a2 s . c o 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; /** * Translate a view to the specified translation values, while rotating to the * specified rotation value. */ public static void addTranslationRotationAnimators(List<Animator> animators, final View view, int xTranslation, int yTranslation, float rotation, int animationDelay) { addXYTranslationAnimators(animators, view, xTranslation, yTranslation, animationDelay); view.setLayerType(View.LAYER_TYPE_HARDWARE, null); view.setRotation(rotation); final ObjectAnimator rotateAnimatorY = ObjectAnimator.ofFloat(view, View.ROTATION, view.getRotation(), 0.0f); rotateAnimatorY.setInterpolator(sDecelerateQuintInterpolator); rotateAnimatorY.setDuration(sAnimationDuration); rotateAnimatorY.setStartDelay(animationDelay); rotateAnimatorY.addListener(new AnimatorListenerAdapter() { private boolean mIsCanceled = false; @Override public void onAnimationCancel(Animator animation) { mIsCanceled = true; } @Override public void onAnimationEnd(Animator animation) { if (!mIsCanceled) { view.setRotation(0); } view.setLayerType(View.LAYER_TYPE_NONE, null); } }); animators.add(rotateAnimatorY); } /** * 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); } }