expand a View with AnimationListener - Android Animation

Android examples for Animation:Expand Animation

Description

expand a View 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 expand(final View v,
            Animation.AnimationListener listener) {
        v.measure(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();
        expandVertical(v, (int) (targetHeight / v.getContext()
                .getResources().getDisplayMetrics().density), listener);
    }//from   w  w w.  ja  va  2s  .  c  om

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