collapse View Animation - Android User Interface

Android examples for User Interface:View Animation

Description

collapse View 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.getHeight();
        final int targetHeight = 1;

        animateHeight(v, initialHeight, targetHeight, View.GONE);
    }//from   www  . j  a  va 2  s . c  o  m

    public static void animateHeight(final View v, final int initialHeight,
            final int targetHeight, final int visibilityAfter) {
        Animation a = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                if (interpolatedTime == 1) {
                    v.getLayoutParams().height = targetHeight;
                    v.setVisibility(visibilityAfter);
                } else {
                    v.getLayoutParams().height = (int) (initialHeight + (targetHeight - initialHeight)
                            * interpolatedTime);
                }
                v.requestLayout();
            }

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

        // 1/4dp / ms
        a.setDuration((int) (targetHeight / v.getContext().getResources()
                .getDisplayMetrics().density));
        v.startAnimation(a);
    }
}

Related Tutorials