collapse a View with custom Animation - Android Animation

Android examples for Animation:Collapse Animation

Description

collapse a View with custom Animation

Demo Code


//package com.java2s;
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 initialHeight = v.getMeasuredHeight();

        Animation a = new Animation() {
            @Override/*from   ww w  .j ava  2  s. c  om*/
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                if (interpolatedTime == 1) {
                    v.setVisibility(View.GONE);
                } else {
                    v.getLayoutParams().height = initialHeight
                            - (int) (initialHeight * interpolatedTime);
                    v.requestLayout();
                }
            }

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

        // 1dp/ms
        a.setDuration((int) (initialHeight / v.getContext().getResources()
                .getDisplayMetrics().density) * 2);
        v.startAnimation(a);
    }
}

Related Tutorials