collapse a View in Vertical for duration - Android Animation

Android examples for Animation:Collapse Animation

Description

collapse a View in Vertical for duration

Demo Code


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

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

public class Main {
    public static void collapseVertical(final View v, int duration,
            final boolean hide, Animation.AnimationListener listener) {
        final int initialHeight = v.getMeasuredHeight();

        Animation a = new AlphaAnimation((float) 1.0, (float) 0.0) {
            @Override//from  w w w.  jav  a 2 s.c om
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                super.applyTransformation(interpolatedTime, t);
                if (interpolatedTime == 1 && hide) {
                    v.setVisibility(View.INVISIBLE);
                } else {
                    v.getLayoutParams().height = initialHeight
                            - (int) (initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

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

        if (listener != null) {
            a.setAnimationListener(listener);
        }
        a.setDuration(duration);
        v.startAnimation(a);
    }
}

Related Tutorials