animate View Height - Android User Interface

Android examples for User Interface:View Animation

Description

animate View Height

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 animateHeight(final View v, final int initialHeight,
            final int targetHeight, final int visibilityAfter) {
        Animation a = new Animation() {
            @Override//from w w w  .  ja v  a  2  s  . co m
            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