Fades a view in and another one out - Android User Interface

Android examples for User Interface:View Fade

Description

Fades a view in and another one out

Demo Code


//package com.java2s;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Context;

import android.view.View;

public class Main {
    /**/* w ww  .j  a  v  a 2 s.  c o  m*/
     * Fades a view in and another one out
     *
     * @param out view being faded out
     * @param in  view being faded in
     */
    public static final void fade(final View out, final View in,
            Context context) {
        fadeOut(out, context);
        fadeIn(in, context);
    }

    /**
     * Fades a view out
     *
     * @param out view being faded out
     */
    public static final void fadeOut(final View out, Context context,
            int visbilityAfter) {
        fadeOut(out, context, 0f, visbilityAfter);
    }

    /**
     * Fades a view out
     *
     * @param out view being faded out
     */
    public static final void fadeOut(final View out, Context context) {
        fadeOut(out, context, 0f, View.GONE);
    }

    /**
     * Fades a view out
     *
     * @param out view being faded out
     */
    public static final void fadeOut(final View out, Context context,
            final float toAlpha, final int visbilityAfter) {
        int shortAnimTime = context.getResources().getInteger(
                android.R.integer.config_shortAnimTime);

        out.setVisibility(View.VISIBLE);
        out.animate().setDuration(shortAnimTime).alpha(toAlpha)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        out.setVisibility(visbilityAfter);
                    }
                });
    }

    /**
     * Fades a view in
     *
     * @param in view being faded in
     */
    public static final void fadeIn(final View in, Context context) {
        int shortAnimTime = context.getResources().getInteger(
                android.R.integer.config_shortAnimTime);
        in.setVisibility(View.VISIBLE);
        in.animate().setDuration(shortAnimTime).alpha(1f)
                .setListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        in.setVisibility(View.VISIBLE);
                    }
                });
    }
}

Related Tutorials