expand a View Vertical with AnimationListener - Android Animation

Android examples for Animation:Expand Animation

Description

expand a View Vertical with AnimationListener

Demo Code


//package com.java2s;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Transformation;

public class Main {
    public static void expandVertical(final View v, int duration,
            Animation.AnimationListener listener) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();

        v.getLayoutParams().height = 0;// ww  w .  j a v a2 s .  com
        v.setVisibility(View.VISIBLE);
        Animation a = new AlphaAnimation((float) 0.0, (float) 1.0) {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                super.applyTransformation(interpolatedTime, t);
                v.getLayoutParams().height = interpolatedTime == 1 ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : (int) (targetHeight * interpolatedTime);
                v.requestLayout();
            }

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

        if (listener != null) {
            a.setAnimationListener(listener);
        }

        // 1dp/ms
        a.setDuration(duration);
        v.startAnimation(a);
    }
}

Related Tutorials