1package com.bumptech.glide.request;
2
3import android.content.Context;
4import android.graphics.drawable.Drawable;
5import android.view.View;
6import android.view.animation.Animation;
7import android.view.animation.AnimationUtils;
8import com.bumptech.glide.request.target.Target;
9
10public class ViewAnimation implements GlideAnimation {
11
12    public static class ViewAnimationFactory implements GlideAnimationFactory {
13        private Animation animation;
14        private Context context;
15        private int animationId;
16        private GlideAnimation glideAnimation;
17
18        public ViewAnimationFactory(Animation animation) {
19            this.animation = animation;
20        }
21
22        public ViewAnimationFactory(Context context, int animationId) {
23            this.context = context;
24            this.animationId = animationId;
25        }
26
27        @Override
28        public GlideAnimation build(boolean isFromMemoryCache, boolean isFirstImage) {
29            if (isFromMemoryCache || !isFirstImage) {
30                return NoAnimation.get();
31            }
32
33            if (glideAnimation == null) {
34                if (animation == null) {
35                    animation = AnimationUtils.loadAnimation(context, animationId);
36                }
37                glideAnimation = new ViewAnimation(animation);
38            }
39
40            return glideAnimation;
41        }
42    }
43
44    private Animation animation;
45
46    public ViewAnimation(Animation animation) {
47        this.animation = animation;
48    }
49
50    @Override
51    public boolean animate(Drawable previous, Object current, View view, Target target) {
52        view.clearAnimation();
53
54        view.startAnimation(animation);
55
56        return false;
57    }
58}
59