Expand a view horizontally from invisible to visible - Android Animation

Android examples for Animation:Slide Animation

Description

Expand a view horizontally from invisible to visible

Demo Code


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

public class Main {

    public static void horizontalExpand(final View viewValue) {
        if (viewValue.getVisibility() == View.VISIBLE) {
            return;
        }//from   w ww .j  a v  a2 s.c  o  m

        viewValue.measure(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        final int targetWidth = viewValue.getMeasuredWidth();

        viewValue.getLayoutParams().width = 0;
        viewValue.setVisibility(View.VISIBLE);

        Animation animation = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation transformation) {
                viewValue.getLayoutParams().width = interpolatedTime == 1 ? ViewGroup.LayoutParams.WRAP_CONTENT
                        : (int) (targetWidth * interpolatedTime);
                viewValue.requestLayout();
            }

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

        animation.setDuration((int) (targetWidth / viewValue.getContext()
                .getResources().getDisplayMetrics().density));
        viewValue.startAnimation(animation);
    }
}

Related Tutorials