animate Expand Actions - Android Animation

Android examples for Animation:Expand Animation

Description

animate Expand Actions

Demo Code


//package com.java2s;

import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;

import android.view.animation.Transformation;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static final long EXPAND_ACTION_DURATION = 150;

    public static void animateExpandActions(final ViewGroup viewGroup,
            boolean skipFirst) {

        final List<View> children = new ArrayList<>(
                viewGroup.getChildCount());

        for (int index = skipFirst ? 1 : 0; index < viewGroup
                .getChildCount(); index++) {
            children.add(viewGroup.getChildAt(index));
        }//from  w w  w  . j  a  va  2s  . c  o  m

        Animation animation;
        final int height = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 48, viewGroup.getContext()
                        .getResources().getDisplayMetrics());
        if (viewGroup.getVisibility() == View.VISIBLE) {
            animation = new Animation() {
                @Override
                protected void applyTransformation(float interpolatedTime,
                        Transformation t) {

                    for (View view : children) {
                        view.setAlpha(1.0f - interpolatedTime);
                    }

                    viewGroup.getLayoutParams().height = (int) (height * (1.0f - interpolatedTime));
                    viewGroup.requestLayout();
                }

                @Override
                public boolean willChangeBounds() {
                    return true;
                }
            };
            animation
                    .setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            viewGroup.setVisibility(View.GONE);
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
        } else {
            animation = new Animation() {
                @Override
                protected void applyTransformation(float interpolatedTime,
                        Transformation t) {

                    for (View view : children) {
                        view.setAlpha(interpolatedTime);
                    }

                    viewGroup.getLayoutParams().height = (int) (interpolatedTime * height);
                    viewGroup.requestLayout();
                }

                @Override
                public boolean willChangeBounds() {
                    return true;
                }
            };
            viewGroup.getLayoutParams().height = 0;
            viewGroup.requestLayout();
            viewGroup.setVisibility(View.VISIBLE);
        }
        animation.setDuration(EXPAND_ACTION_DURATION);
        animation.setInterpolator(new DecelerateInterpolator());
        viewGroup.startAnimation(animation);
        viewGroup.requestLayout();
    }
}

Related Tutorials