apply alpha animation to given view and set visibility to gone or visible - Android android.view.animation

Android examples for android.view.animation:Color Animation

Description

apply alpha animation to given view and set visibility to gone or visible

Demo Code

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;

public class Main{

    /**//w w w .java  2 s.c o m
     * apply alpha animation to given view and set visibility to gone or visible
     * @param view
     * @param from have to be 1 or 0
     * @param to have to be 1 or 0
     * @param duration
     * @return ObjectAnimator
     */
    public static ObjectAnimator fadeThenGoneOrVisible(final View view,
            float from, final float to, int duration) {

        if (from == 0) {
            view.setVisibility(View.VISIBLE);
        }

        ObjectAnimator alpha = ObjectAnimator.ofFloat(view, "alpha", from,
                to);
        alpha.setDuration(duration);

        alpha.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                if (to == 0) {
                    view.setVisibility(View.GONE);
                }
            }
        });

        alpha.start();

        return alpha;

    }

}

Related Tutorials