aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/view/FadeOutAnimation.java
blob: 817839ef2d452ab9523a481f747988c242ab203b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 This file is part of Subsonic.

 Subsonic is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 Subsonic is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with Subsonic.  If not, see <http://www.gnu.org/licenses/>.

 Copyright 2009 (C) Sindre Mehus
 */
package github.daneren2005.dsub.view;

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

/**
 * Fades a view out by changing its alpha value.
 *
 * @author Sindre Mehus
 * @version $Id: Util.java 3203 2012-10-04 09:12:08Z sindre_mehus $
 */
public class FadeOutAnimation extends AlphaAnimation {

    private boolean cancelled;

    /**
     * Creates and starts the fade out animation.
     *
     * @param view           The view to fade out (or display).
     * @param fadeOut        If true, the view is faded out. Otherwise it is immediately made visible.
     * @param durationMillis Fade duration.
     */
    public static void createAndStart(View view, boolean fadeOut, long durationMillis) {
        if (fadeOut) {
            view.clearAnimation();
            view.startAnimation(new FadeOutAnimation(view, durationMillis));
        } else {
            Animation animation = view.getAnimation();
            if (animation instanceof FadeOutAnimation) {
                ((FadeOutAnimation) animation).cancelFadeOut();
            }
            view.clearAnimation();
            view.setVisibility(View.VISIBLE);
        }
    }

    FadeOutAnimation(final View view, long durationMillis) {
        super(1.0F, 0.0F);
        setDuration(durationMillis);
        setAnimationListener(new AnimationListener() {
            public void onAnimationStart(Animation animation) {
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationEnd(Animation animation) {
                if (!cancelled) {
                    view.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    private void cancelFadeOut() {
        cancelled = true;
    }
}