set View Visibility for duration - Android User Interface

Android examples for User Interface:View Hide Show

Description

set View Visibility for duration

Demo Code


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

import android.view.View;

import android.view.animation.AccelerateDecelerateInterpolator;

public class Main {
    public static void setVisibility(View view, int visibility) {
        setVisibility(view, visibility, 300l);
    }//w  w w.  ja  va2 s.c  o  m

    public static void setVisibility(final View view, final int visibility,
            long duration) {
        if (view.getVisibility() == visibility) {
            return;
        }
        if (view.getVisibility() != View.VISIBLE
                && visibility == View.VISIBLE) {
            view.setAlpha(0f);
            view.setVisibility(View.VISIBLE);
            view.animate()
                    .alpha(1f)
                    .setInterpolator(new AccelerateDecelerateInterpolator())
                    .setDuration(duration).setListener(null).start();

        } else if (view.getVisibility() == View.VISIBLE) {
            view.animate()
                    .alpha(0f)
                    .setInterpolator(new AccelerateDecelerateInterpolator())
                    .setDuration(duration)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            view.setVisibility(visibility);
                            view.setAlpha(1f);
                        }
                    }).start();
        }
    }
}

Related Tutorials