Home | History | Annotate | Download | only in animation
      1 package com.bumptech.glide.request.animation;
      2 
      3 import android.content.Context;
      4 import android.view.animation.Animation;
      5 import android.view.animation.AnimationUtils;
      6 
      7 /**
      8  * A {@link com.bumptech.glide.request.animation.GlideAnimationFactory} that produces
      9  * {@link com.bumptech.glide.request.animation.ViewAnimation}s.
     10  *
     11  * @param <R> The type of the resource displayed in the view that is animated
     12  */
     13 public class ViewAnimationFactory<R> implements GlideAnimationFactory<R> {
     14     private final ViewAnimation.AnimationFactory animationFactory;
     15     private GlideAnimation<R> glideAnimation;
     16 
     17     public ViewAnimationFactory(Animation animation) {
     18         this(new ConcreteAnimationFactory(animation));
     19     }
     20 
     21     public ViewAnimationFactory(Context context, int animationId) {
     22         this(new ResourceAnimationFactory(context, animationId));
     23     }
     24 
     25     ViewAnimationFactory(ViewAnimation.AnimationFactory animationFactory) {
     26         this.animationFactory = animationFactory;
     27     }
     28 
     29     /**
     30      * Returns a new {@link com.bumptech.glide.request.animation.GlideAnimation} for the given arguments. If
     31      * isFromMemoryCache is {@code true} or isFirstImage is {@code false}, returns a
     32      * {@link com.bumptech.glide.request.animation.NoAnimation} and otherwise returns a new
     33      * {@link com.bumptech.glide.request.animation.ViewAnimation}.
     34      *
     35      * @param isFromMemoryCache {@inheritDoc}
     36      * @param isFirstResource   {@inheritDoc}
     37      */
     38     @Override
     39     public GlideAnimation<R> build(boolean isFromMemoryCache, boolean isFirstResource) {
     40         if (isFromMemoryCache || !isFirstResource) {
     41             return NoAnimation.get();
     42         }
     43 
     44         if (glideAnimation == null) {
     45             glideAnimation = new ViewAnimation<R>(animationFactory);
     46         }
     47 
     48         return glideAnimation;
     49     }
     50 
     51     private static class ConcreteAnimationFactory implements ViewAnimation.AnimationFactory {
     52         private final Animation animation;
     53 
     54         public ConcreteAnimationFactory(Animation animation) {
     55             this.animation = animation;
     56         }
     57 
     58         @Override
     59         public Animation build() {
     60             return animation;
     61         }
     62     }
     63 
     64     private static class ResourceAnimationFactory implements ViewAnimation.AnimationFactory {
     65         private final Context context;
     66         private final int animationId;
     67 
     68         public ResourceAnimationFactory(Context context, int animationId) {
     69             this.context = context.getApplicationContext();
     70             this.animationId = animationId;
     71         }
     72 
     73         @Override
     74         public Animation build() {
     75             return AnimationUtils.loadAnimation(context, animationId);
     76         }
     77     }
     78 }
     79