collapse a View to hide or show - Android Animation

Android examples for Animation:Collapse Animation

Description

collapse a View to hide or show

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 collapse(final View v, boolean hide,
            Animation.AnimationListener listener) {
        final int initialHeight = v.getMeasuredHeight();
        collapseVertical(v, (int) (initialHeight / v.getContext()
                .getResources().getDisplayMetrics().density), hide,
                listener);/*from w w  w .j ava2s  .c  om*/
    }

    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
            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