Performs a stacked card animation that brings a bottom card to the front In preparation two views should be stacked on top of each other with appropriate margin so that the bottom card sticks out on the bottom and the right. - Android User Interface

Android examples for User Interface:View Translate

Description

Performs a stacked card animation that brings a bottom card to the front In preparation two views should be stacked on top of each other with appropriate margin so that the bottom card sticks out on the bottom and the right.

Demo Code

import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;

public class Main {
  /**//from   ww w .ja va 2 s .  c om
   * Performs a stacked card animation that brings a bottom card to the front In
   * preparation two views should be stacked on top of each other with
   * appropriate margin so that the bottom card sticks out on the bottom and the
   * right.
   *
   * @param topCard
   * @param bottomCard
   * @param topCardElevation
   * @param bottomCardElevation
   * @param leftToRight
   *          indicates which direction the animation of the top card should go.
   * @param listener
   */
  public static void animateSwapCards(final View topCard,
      final View bottomCard, final int topCardElevation,
      final int bottomCardElevation, final boolean leftToRight,
      Animation.AnimationListener listener) {
    long duration = 400;
    float xMargin = topCard.getX() - bottomCard.getX();
    float yMargin = topCard.getY() - bottomCard.getY();

    topCard.clearAnimation();
    bottomCard.clearAnimation();

    final ViewGroup.LayoutParams topLayout = topCard.getLayoutParams();
    final ViewGroup.LayoutParams bottomLayout = bottomCard.getLayoutParams();

    // bottom animation
    Animation upLeft = new TranslateAnimation(0f, xMargin, 0f, yMargin);
    upLeft.setDuration(duration);
    Animation bottomCardRight = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, .5f,
        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
    bottomCardRight.setDuration(duration);
    Animation bottomCardLeft = new TranslateAnimation(
        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -.5f,
        Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
    bottomCardLeft.setDuration(duration);

    AnimationSet bottomOutSet = new AnimationSet(false);
    if (leftToRight) {
      bottomOutSet.addAnimation(bottomCardLeft);
    } else {
      bottomOutSet.addAnimation(bottomCardRight);
    }
    AnimationSet bottomInSet = new AnimationSet(false);
    bottomInSet.setStartOffset(duration);
    if (leftToRight) {
      bottomInSet.addAnimation(bottomCardRight);
      bottomInSet.addAnimation(upLeft);
    } else {
      bottomInSet.addAnimation(bottomCardLeft);
      bottomInSet.addAnimation(upLeft);
    }
    AnimationSet bottomSet = new AnimationSet(false);
    bottomSet.setInterpolator(new LinearInterpolator());
    bottomSet.addAnimation(bottomOutSet);
    bottomSet.addAnimation(bottomInSet);
    bottomSet.setAnimationListener(listener);

    bottomOutSet.setAnimationListener(new Animation.AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) {

      }

      @Override
      public void onAnimationEnd(Animation animation) {
        // elevation takes precedence for API 21+
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          topCard.setElevation(bottomCardElevation);
          bottomCard.setElevation(topCardElevation);
        }
        bottomCard.bringToFront();
        ((View) bottomCard.getParent()).requestLayout();
        ((View) bottomCard.getParent()).invalidate();
      }

      @Override
      public void onAnimationRepeat(Animation animation) {

      }
    });
    bottomInSet.setAnimationListener(new Animation.AnimationListener() {
      @Override
      public void onAnimationStart(Animation animation) {

      }

      @Override
      public void onAnimationEnd(Animation animation) {
        bottomCard.clearAnimation();
        bottomCard.setLayoutParams(topLayout);
        topCard.clearAnimation();
        topCard.setLayoutParams(bottomLayout);
      }

      @Override
      public void onAnimationRepeat(Animation animation) {

      }
    });

    // top animation
    Animation downRight = new TranslateAnimation(0f, -xMargin, 0f, -yMargin);
    downRight.setDuration(duration);
    Animation topCardRight = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
        0, Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, 0,
        Animation.RELATIVE_TO_SELF, 0);
    topCardRight.setDuration(duration);
    Animation topCardLeft = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
        0, Animation.RELATIVE_TO_SELF, -.5f, Animation.RELATIVE_TO_SELF, 0,
        Animation.RELATIVE_TO_SELF, 0);
    topCardLeft.setDuration(duration);

    AnimationSet topOutSet = new AnimationSet(false);
    if (leftToRight) {
      topOutSet.addAnimation(topCardRight);
    } else {
      topOutSet.addAnimation(topCardLeft);
    }
    AnimationSet topInSet = new AnimationSet(false);
    topInSet.setStartOffset(duration);
    if (leftToRight) {
      topInSet.addAnimation(topCardLeft);
      topInSet.addAnimation(downRight);
    } else {
      topInSet.addAnimation(topCardRight);
      topInSet.addAnimation(downRight);
    }

    AnimationSet topSet = new AnimationSet(false);
    topSet.setInterpolator(new LinearInterpolator());
    topSet.addAnimation(topOutSet);
    topSet.addAnimation(topInSet);

    // start animations
    bottomCard.startAnimation(bottomSet);
    topCard.startAnimation(topSet);
  }
}

Related Tutorials