Expand a view vertically from invisible to visible - Android Animation

Android examples for Animation:Slide Animation

Description

Expand a view vertically from invisible to visible

Demo Code


//package com.java2s;
import android.view.View;

import android.view.animation.Animation;
import android.view.animation.Transformation;

public class Main {
    /**//  w  ww.  ja  v a2  s.c om
     * Expand a view vertically from invisible to visible
     *
     * @param viewValue
     */
    public static void horizontalCollapse(final View viewValue) {
        // validate if the view is already expanded
        if (viewValue.getVisibility() == View.GONE) {
            return;
        }

        // get and keep initial view width
        final int initialWidth = viewValue.getMeasuredWidth();

        // create a new animation
        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation transformation) {
                // validate time
                if (interpolatedTime == 1) {
                    viewValue.setVisibility(View.GONE);
                } else {
                    viewValue.getLayoutParams().width = initialWidth
                            - (int) (initialWidth * interpolatedTime);
                    viewValue.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };

        // execute the animation 1dp by milliseconds
        animation.setDuration((int) (initialWidth / viewValue.getContext()
                .getResources().getDisplayMetrics().density));
        viewValue.startAnimation(animation);
    }
}

Related Tutorials