collapse View via animation - Android User Interface

Android examples for User Interface:View Animation

Description

collapse View via 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 final long DEFAULT_DURATION = 300;

    public static void collapseView(final View v) {
        collapseView(v, DEFAULT_DURATION);
    }/*from  w w  w .j ava 2s .co  m*/

    public static void collapseView(final View v, long duration) {
        final int initialHeight = v.getMeasuredHeight();

        Animation a = new Animation() {
            @Override
            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;
            }
        };

        a.setDuration(duration);
        v.startAnimation(a);
    }
}

Related Tutorials