Create collapse effect animation for a View - Android android.view.animation

Android examples for android.view.animation:Effect Animation

Description

Create collapse effect animation for a View

Demo Code

import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class Main{

    public static void collapse(final View v, final int minHeight) {
        final int initialHeight = v.getMeasuredHeight();

        final int offset = initialHeight - minHeight;
        Animation a = new Animation() {
            @Override//from  w ww  . j  a v a 2 s  .co m
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                if (interpolatedTime == 1) {
                    v.getLayoutParams().height = minHeight;
                    v.requestLayout();
                } else {
                    v.getLayoutParams().height = initialHeight
                            - (int) (offset * interpolatedTime);
                    v.requestLayout();
                }
            }

            @Override
            public boolean willChangeBounds() {
                return true;
            }
        };
        // 1dp/ms
        a.setDuration((int) (minHeight / v.getContext().getResources()
                .getDisplayMetrics().density));
        v.startAnimation(a);
    }

}

Related Tutorials