Create android.animation.Animator object that contains animation that will detach selected view from selected parent with animation - Android android.view.animation

Android examples for android.view.animation:ObjectAnimator

Description

Create android.animation.Animator object that contains animation that will detach selected view from selected parent with animation

Demo Code

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.view.View;
import android.view.ViewGroup;

public class Main {

  /**/*from w ww .  j  a  v  a2  s . com*/
   * Prepare an {@link android.animation.Animator} object that contains animation
   * that will detach selected view from selected parent with animation
   *
   * @param parent
   * @param view
   * @param duration
   * @return
   */
  public static Animator prepareDetachViewAnimated(ViewGroup parent, View view, int duration) {
    return prepareShowViewAnimated(false, parent, view, duration);
  }

  /**
   * Prepare to show view animated and decide whether to attach/detach our view to
   * our parent before/after the animation
   *
   * @param show
   * @param parent
   * @param view
   * @param duration
   */
  private static Animator prepareShowViewAnimated(boolean show, final ViewGroup parent, final View view, int duration) {
    Animator anim = null;
    if (view == null) {
      return anim;
    }

    if (show && view.getVisibility() != View.VISIBLE) {
      // show
      view.setEnabled(true);
      view.setVisibility(View.VISIBLE);
      if (parent != null) {
        // attach view to its parent
        parent.addView(view);
      }

      anim = ObjectAnimator.ofFloat(view, "alpha", 0, 1).setDuration(duration);
    } else if (!show && view.getVisibility() == View.VISIBLE) {
      // hide
      view.setEnabled(false);
      anim = ObjectAnimator.ofFloat(view, "alpha", 1, 0).setDuration(duration);
      anim.addListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationEnd(Animator animation) {
          view.setVisibility(View.GONE);

          if (parent != null) {
            // detach view from its parent
            parent.removeView(view);
          }
        }

        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
      });
    }

    return anim;
  }

  /**
   * Prepare an {@link android.animation.Animator} object that contains animation
   * that will display selected view
   *
   * @param view
   * @param duration
   * @return
   */
  public static Animator prepareShowViewAnimated(View view, int duration) {
    return prepareShowViewAnimated(true, null, view, duration);
  }

}

Related Tutorials