Cross animate two views, showing one, hiding the other. - Android Animation

Android examples for Animation:Animation to Show

Description

Cross animate two views, showing one, hiding the other.

Demo Code


//package com.java2s;

import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.view.View;

public class Main {
    public static final int ANIMATION_DURATION_SHORTEST = 150;
    public static final int ANIMATION_DURATION_SHORT = 250;

    public static void crossFadeViews(View showView, View hideView) {
        crossFadeViews(showView, hideView, ANIMATION_DURATION_SHORT);
    }/* ww w.  j a  v  a2  s  .  co m*/

    public static void crossFadeViews(View showView, final View hideView,
            int duration) {
        fadeInView(showView, duration);
        fadeOutView(hideView, duration);
    }

    public static void fadeInView(View view) {
        fadeInView(view, ANIMATION_DURATION_SHORTEST);
    }

    public static void fadeInView(View view, int duration) {
        view.setVisibility(View.VISIBLE);
        view.setAlpha(0f);

        // Setting the listener to null, so it won't keep getting called.
        ViewCompat.animate(view).alpha(1f).setDuration(duration)
                .setListener(null);
    }
    public static void fadeOutView(View view) {
        fadeOutView(view, ANIMATION_DURATION_SHORTEST);
    }
    public static void fadeOutView(final View view, int duration) {
        ViewCompat.animate(view).alpha(0f).setDuration(duration)
                .setListener(new ViewPropertyAnimatorListener() {
                    @Override
                    public void onAnimationStart(View view) {
                        view.setDrawingCacheEnabled(true);
                    }

                    @Override
                    public void onAnimationEnd(View view) {
                        view.setVisibility(View.GONE);
                        view.setAlpha(1f);
                        view.setDrawingCacheEnabled(false);
                    }

                    @Override
                    public void onAnimationCancel(View view) {
                    }
                });
    }
}

Related Tutorials