aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/request/animation/ViewAnimation.java
blob: 7951b5cf5dd66f9868a3f9883465e62d1dd184da (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
78
79
80
81
82
83
84
85
86
87
88
89
package com.bumptech.glide.request.animation;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

/**
 * An {@link com.bumptech.glide.request.animation.GlideAnimation} that can apply a
 * {@link android.view.animation.Animation} to a {@link android.view.View} using
 * {@link android.view.View#startAnimation(android.view.animation.Animation)}.
 */
public class ViewAnimation implements GlideAnimation {

    /**
     * A {@link com.bumptech.glide.request.animation.GlideAnimationFactory} that produces ViewAnimations.
     */
    public static class ViewAnimationFactory implements GlideAnimationFactory {
        private Animation animation;
        private Context context;
        private int animationId;
        private GlideAnimation glideAnimation;

        public ViewAnimationFactory(Animation animation) {
            this.animation = animation;
        }

        public ViewAnimationFactory(Context context, int animationId) {
            this.context = context;
            this.animationId = animationId;
        }

        /**
         * Returns a new {@link com.bumptech.glide.request.animation.GlideAnimation} for the given arguments. If
         * isFromMemoryCache is {@code true} or isFirstImage is {@code false}, returns a
         * {@link com.bumptech.glide.request.animation.NoAnimation} and otherwise returns a new
         * {@link com.bumptech.glide.request.animation.ViewAnimation}.
         *
         * @param isFromMemoryCache {@inheritDoc}
         * @param isFirstImage {@inheritDoc}
         */
        @Override
        public GlideAnimation build(boolean isFromMemoryCache, boolean isFirstImage) {
            if (isFromMemoryCache || !isFirstImage) {
                return NoAnimation.get();
            }

            if (glideAnimation == null) {
                if (animation == null) {
                    animation = AnimationUtils.loadAnimation(context, animationId);
                }
                glideAnimation = new ViewAnimation(animation);
            }

            return glideAnimation;
        }
    }

    private Animation animation;

    /**
     * Constructs a new ViewAnimation that will start the given {@link android.view.animation.Animation}.
     * @param animation The animation to use.
     */
    public ViewAnimation(Animation animation) {
        this.animation = animation;
    }

    /**
     * Always clears the current animation on the view using {@link android.view.View#clearAnimation()}, then
     * starts the {@link android.view.animation.Animation} given in the constructor using
     * {@link android.view.View#startAnimation(android.view.animation.Animation)} and then returns {@code false} because
     * the animation does not actually set the current resource on the view.
     *
     * @param previous {@inheritDoc}
     * @param current {@inheritDoc}
     * @param view {@inheritDoc}
     * @return {@inheritDoc}
     */
    @Override
    public boolean animate(Drawable previous, Object current, ImageView view) {
        view.clearAnimation();

        view.startAnimation(animation);

        return false;
    }
}