Home | History | Annotate | Download | only in drawable
      1 package com.bumptech.glide.load.resource.drawable;
      2 
      3 import android.graphics.drawable.Drawable;
      4 
      5 import com.bumptech.glide.load.engine.Resource;
      6 
      7 /**
      8  * Simple wrapper for an Android {@link Drawable} which returns a
      9  * {@link android.graphics.drawable.Drawable.ConstantState#newDrawable() new drawable}
     10  * based on it's {@link android.graphics.drawable.Drawable.ConstantState state}.
     11  *
     12  * <b>Suggested usages only include {@code T}s where the new drawable is of the same or descendant class.</b>
     13  *
     14  * @param <T> type of the wrapped {@link Drawable}
     15  */
     16 public abstract class DrawableResource<T extends Drawable> implements Resource<T> {
     17     protected final T drawable;
     18     private boolean returnedOriginalDrawable;
     19 
     20     public DrawableResource(T drawable) {
     21         if (drawable == null) {
     22             throw new NullPointerException("Drawable must not be null!");
     23         }
     24         this.drawable = drawable;
     25     }
     26 
     27     @SuppressWarnings("unchecked")
     28     // drawables should always return a copy of the same class
     29     @Override
     30     public final T get() {
     31         if (!returnedOriginalDrawable) {
     32             returnedOriginalDrawable = true;
     33             return drawable;
     34         } else {
     35             return (T) drawable.getConstantState().newDrawable();
     36         }
     37     }
     38 }
     39