expand View Animation - Android User Interface

Android examples for User Interface:View Scale

Description

expand View Animation

Demo Code


//package com.java2s;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;

public class Main {
    public static void expand(final View v) {
        final int initialHeight = v.getHeight();
        v.measure(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        final int targetHeight = v.getMeasuredHeight();

        animateHeight(v, initialHeight, targetHeight, View.VISIBLE);
    }/*from   www.  j a  v  a  2s .c  om*/

    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