Shrink one View and then grow another View - Android User Interface

Android examples for User Interface:View

Description

Shrink one View and then grow another View

Demo Code


//package com.java2s;

import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;

public class Main {
    public static final int DEFAULT_SHRINK_GROW_DURATION = 200;

    /**// w w w  .j  a  v  a2  s  .com
     * Shrink view1 and then grow view2
     *
     * @param view1 view to shrink
     * @param view2 view to grow
     * @param duration duration for each phase of the animation
     */
    public static void shrinkAndGrow(final View view1, final View view2,
            int duration) {
        ScaleAnimation shrinkAnim = new ScaleAnimation(1, 0, 1, 0,
                view1.getWidth() / 2, view1.getHeight() / 2);
        final ScaleAnimation growAnim = new ScaleAnimation(0, 1, 0, 1,
                view2.getWidth() / 2, view2.getHeight() / 2);
        shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view1.setVisibility(View.INVISIBLE);
                view2.setVisibility(View.VISIBLE);
                view2.startAnimation(growAnim);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        shrinkAnim.setDuration(duration);
        growAnim.setDuration(duration);
        view1.startAnimation(shrinkAnim);
    }

    /**
     * Shrink and grow with default duration
     *
     * @param view1
     * @param view2
     */
    public static void shrinkAndGrow(View view1, View view2) {
        shrinkAndGrow(view1, view2, DEFAULT_SHRINK_GROW_DURATION);
    }
}

Related Tutorials