animate Expand - Android Animation

Android examples for Animation:Expand Animation

Description

animate Expand

Demo Code


//package com.java2s;

import android.view.View;

import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;

import android.view.animation.Transformation;

public class Main {
    public static final long EXPAND_ACTION_DURATION = 150;

    public static void animateExpand(final View view) {

        final int height = view.getMeasuredHeight();

        Animation animation;//ww w  .ja va  2  s .c  om
        if (view.getVisibility() == View.VISIBLE) {
            animation = new Animation() {
                @Override
                protected void applyTransformation(float interpolatedTime,
                        Transformation t) {
                    view.getLayoutParams().height = (int) (height * (1.0f - interpolatedTime));
                    view.requestLayout();
                }

                @Override
                public boolean willChangeBounds() {
                    return true;
                }
            };
            animation
                    .setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            view.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
        } else {
            animation = new Animation() {
                @Override
                protected void applyTransformation(float interpolatedTime,
                        Transformation t) {
                    view.getLayoutParams().height = (int) (interpolatedTime * height);
                    view.requestLayout();
                }

                @Override
                public boolean willChangeBounds() {
                    return true;
                }
            };
            view.getLayoutParams().height = 0;
            view.requestLayout();
            view.setVisibility(View.VISIBLE);
        }
        animation.setDuration(EXPAND_ACTION_DURATION);
        animation.setInterpolator(new DecelerateInterpolator());
        view.startAnimation(animation);
        view.requestLayout();
    }
}

Related Tutorials