Home | History | Annotate | Download | only in glide
      1 package com.bumptech.glide;
      2 
      3 import android.content.Context;
      4 import android.graphics.Bitmap;
      5 import android.graphics.drawable.Drawable;
      6 import android.view.animation.Animation;
      7 import android.widget.ImageView;
      8 
      9 import com.bumptech.glide.load.Encoder;
     10 import com.bumptech.glide.load.Key;
     11 import com.bumptech.glide.load.ResourceDecoder;
     12 import com.bumptech.glide.load.ResourceEncoder;
     13 import com.bumptech.glide.load.Transformation;
     14 import com.bumptech.glide.load.engine.DiskCacheStrategy;
     15 import com.bumptech.glide.load.model.ImageVideoWrapper;
     16 import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
     17 import com.bumptech.glide.load.resource.drawable.GlideDrawable;
     18 import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper;
     19 import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapperTransformation;
     20 import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;
     21 import com.bumptech.glide.manager.Lifecycle;
     22 import com.bumptech.glide.manager.RequestTracker;
     23 import com.bumptech.glide.provider.LoadProvider;
     24 import com.bumptech.glide.request.RequestListener;
     25 import com.bumptech.glide.request.animation.DrawableCrossFadeFactory;
     26 import com.bumptech.glide.request.animation.ViewPropertyAnimation;
     27 import com.bumptech.glide.request.target.Target;
     28 
     29 import java.io.File;
     30 
     31 /**
     32  * A class for creating a request to load a {@link GlideDrawable}.
     33  *
     34  * <p>
     35  *     Warning - It is <em>not</em> safe to use this builder after calling <code>into()</code>, it may be pooled and
     36  *     reused.
     37  * </p>
     38  *
     39  * @param  The type of model that will be loaded into the target.
     40  */
     41 public class DrawableRequestBuilder<ModelType>
     42         extends GenericRequestBuilder<ModelType, ImageVideoWrapper, GifBitmapWrapper, GlideDrawable>
     43         implements BitmapOptions, DrawableOptions {
     44 
     45     DrawableRequestBuilder(Context context, Class<ModelType> modelClass,
     46             LoadProvider<ModelType, ImageVideoWrapper, GifBitmapWrapper, GlideDrawable> loadProvider, Glide glide,
     47             RequestTracker requestTracker, Lifecycle lifecycle) {
     48         super(context, modelClass, loadProvider, GlideDrawable.class, glide, requestTracker, lifecycle);
     49         // Default to animating.
     50         crossFade();
     51     }
     52 
     53     /**
     54      * Loads and displays the {@link GlideDrawable} retrieved by the given thumbnail request if it finishes before this
     55      * request. Best used for loading thumbnail {@link GlideDrawable}s that are smaller and will be loaded more quickly
     56      * than the fullsize {@link GlideDrawable}. There are no guarantees about the order in which the requests will
     57      * actually finish. However, if the thumb request completes after the full request, the thumb {@link GlideDrawable}
     58      * will never replace the full image.
     59      *
     60      * @see #thumbnail(float)
     61      *
     62      * <p>
     63      *     Note - Any options on the main request will not be passed on to the thumbnail request. For example, if
     64      *     you want an animation to occur when either the full {@link GlideDrawable} loads or the thumbnail loads,
     65      *     you need to call {@link #animate(int)} on both the thumb and the full request. For a simpler thumbnail
     66      *     option where these options are applied to the humbnail as well, see {@link #thumbnail(float)}.
     67      * </p>
     68      *
     69      * <p>
     70      *     Only the thumbnail call on the main request will be obeyed, recursive calls to this method are ignored.
     71      * </p>
     72      *
     73      * @param thumbnailRequest The request to use to load the thumbnail.
     74      * @return This builder object.
     75      */
     76     public DrawableRequestBuilder<ModelType> thumbnail(
     77             DrawableRequestBuilder<?> thumbnailRequest) {
     78         super.thumbnail(thumbnailRequest);
     79         return this;
     80     }
     81 
     82     /**
     83      * {@inheritDoc}
     84      */
     85     @Override
     86     public DrawableRequestBuilder<ModelType> thumbnail(
     87             GenericRequestBuilder<?, ?, ?, GlideDrawable> thumbnailRequest) {
     88         super.thumbnail(thumbnailRequest);
     89         return this;
     90     }
     91 
     92     /**
     93      * {@inheritDoc}
     94      */
     95     @Override
     96     public DrawableRequestBuilder<ModelType> thumbnail(float sizeMultiplier) {
     97         super.thumbnail(sizeMultiplier);
     98         return this;
     99     }
    100 
    101     /**
    102      * {@inheritDoc}
    103      */
    104     @Override
    105     public DrawableRequestBuilder<ModelType> sizeMultiplier(float sizeMultiplier) {
    106         super.sizeMultiplier(sizeMultiplier);
    107         return this;
    108     }
    109 
    110     /**
    111      * {@inheritDoc}
    112      */
    113     @Override
    114     public DrawableRequestBuilder<ModelType> decoder(ResourceDecoder<ImageVideoWrapper, GifBitmapWrapper> decoder) {
    115         super.decoder(decoder);
    116         return this;
    117     }
    118 
    119     /**
    120      * {@inheritDoc}
    121      */
    122     @Override
    123     public DrawableRequestBuilder<ModelType> cacheDecoder(ResourceDecoder<File, GifBitmapWrapper> cacheDecoder) {
    124         super.cacheDecoder(cacheDecoder);
    125         return this;
    126     }
    127 
    128     /**
    129      * {@inheritDoc}
    130      */
    131     @Override
    132     public DrawableRequestBuilder<ModelType> encoder(ResourceEncoder<GifBitmapWrapper> encoder) {
    133         super.encoder(encoder);
    134         return this;
    135     }
    136 
    137     /**
    138      * {@inheritDoc}
    139      */
    140     @Override
    141     public DrawableRequestBuilder<ModelType> priority(Priority priority) {
    142         super.priority(priority);
    143         return this;
    144     }
    145 
    146     /**
    147      * Transform {@link GlideDrawable}s using the given
    148      * {@link com.bumptech.glide.load.resource.bitmap.BitmapTransformation}s.
    149      *
    150      * <p>
    151      *     Note - Bitmap transformations will apply individually to each frame of animated GIF images and also to
    152      *     individual {@link Bitmap}s.
    153      * </p>
    154      *
    155      * @see #centerCrop()
    156      * @see #fitCenter()
    157      * @see #bitmapTransform(com.bumptech.glide.load.Transformation[])
    158      * @see #transform(com.bumptech.glide.load.Transformation[])
    159      *
    160      * @param transformations The transformations to apply in order.
    161      * @return This request builder.
    162      */
    163     public DrawableRequestBuilder<ModelType> transform(BitmapTransformation... transformations) {
    164         return bitmapTransform(transformations);
    165     }
    166 
    167     /**
    168      * Transform {@link GlideDrawable}s using {@link com.bumptech.glide.load.resource.bitmap.CenterCrop}.
    169      *
    170      * @see #fitCenter()
    171      * @see #transform(com.bumptech.glide.load.resource.bitmap.BitmapTransformation...)
    172      * @see #bitmapTransform(com.bumptech.glide.load.Transformation[])
    173      * @see #transform(com.bumptech.glide.load.Transformation[])
    174      *
    175      * @return This request builder.
    176      */
    177     @SuppressWarnings("unchecked")
    178     public DrawableRequestBuilder<ModelType> centerCrop() {
    179         return transform(glide.getDrawableCenterCrop());
    180     }
    181 
    182     /**
    183      * Transform {@link GlideDrawable}s using {@link com.bumptech.glide.load.resource.bitmap.FitCenter}.
    184      *
    185      * @see #centerCrop()
    186      * @see #transform(com.bumptech.glide.load.resource.bitmap.BitmapTransformation...)
    187      * @see #bitmapTransform(com.bumptech.glide.load.Transformation[])
    188      * @see #transform(com.bumptech.glide.load.Transformation[])
    189      *
    190      * @return This request builder.
    191      */
    192     @SuppressWarnings("unchecked")
    193     public DrawableRequestBuilder<ModelType> fitCenter() {
    194         return transform(glide.getDrawableFitCenter());
    195     }
    196 
    197     /**
    198      * Transform {@link GlideDrawable}s using the given {@link android.graphics.Bitmap} transformations. Replaces any
    199      * previous transformations.
    200      *
    201      * @see #fitCenter()
    202      * @see #centerCrop()
    203      * @see #transform(com.bumptech.glide.load.resource.bitmap.BitmapTransformation...)
    204      * @see #transform(com.bumptech.glide.load.Transformation[])
    205      *
    206      * @return This request builder.
    207      */
    208     public DrawableRequestBuilder<ModelType> bitmapTransform(Transformation<Bitmap>... bitmapTransformations) {
    209         GifBitmapWrapperTransformation[] transformations =
    210                 new GifBitmapWrapperTransformation[bitmapTransformations.length];
    211         for (int i = 0; i < bitmapTransformations.length; i++) {
    212             transformations[i] = new GifBitmapWrapperTransformation(glide.getBitmapPool(), bitmapTransformations[i]);
    213         }
    214         return transform(transformations);
    215     }
    216 
    217 
    218 
    219     /**
    220      * {@inheritDoc}
    221      *
    222      * @see #bitmapTransform(com.bumptech.glide.load.Transformation[])
    223      * @see #centerCrop()
    224      * @see #fitCenter()
    225      */
    226     @Override
    227     public DrawableRequestBuilder<ModelType> transform(Transformation<GifBitmapWrapper>... transformation) {
    228         super.transform(transformation);
    229         return this;
    230     }
    231 
    232     /**
    233      * {@inheritDoc}
    234      */
    235     @Override
    236     public DrawableRequestBuilder<ModelType> transcoder(
    237             ResourceTranscoder<GifBitmapWrapper, GlideDrawable> transcoder) {
    238         super.transcoder(transcoder);
    239         return this;
    240     }
    241 
    242     /**
    243      * {@inheritDoc}
    244      */
    245     public final DrawableRequestBuilder<ModelType> crossFade() {
    246         super.animate(new DrawableCrossFadeFactory<GlideDrawable>());
    247         return this;
    248     }
    249 
    250     /**
    251      * {@inheritDoc}
    252      */
    253     public DrawableRequestBuilder<ModelType> crossFade(int duration) {
    254         super.animate(new DrawableCrossFadeFactory<GlideDrawable>(duration));
    255         return this;
    256     }
    257 
    258     /**
    259      * {@inheritDoc}
    260      */
    261     @Deprecated
    262     public DrawableRequestBuilder<ModelType> crossFade(Animation animation, int duration) {
    263         super.animate(new DrawableCrossFadeFactory<GlideDrawable>(animation, duration));
    264         return this;
    265     }
    266 
    267     /**
    268      * {@inheritDoc}
    269      */
    270     public DrawableRequestBuilder<ModelType> crossFade(int animationId, int duration) {
    271         super.animate(new DrawableCrossFadeFactory<GlideDrawable>(context, animationId,
    272                 duration));
    273         return this;
    274     }
    275 
    276     /**
    277      * {@inheritDoc}
    278      */
    279     @Override
    280     public DrawableRequestBuilder<ModelType> dontAnimate() {
    281         super.dontAnimate();
    282         return this;
    283     }
    284 
    285     /**
    286      * {@inheritDoc}
    287      */
    288     @Override
    289     public DrawableRequestBuilder<ModelType> animate(ViewPropertyAnimation.Animator animator) {
    290         super.animate(animator);
    291         return this;
    292     }
    293 
    294     /**
    295      * {@inheritDoc}
    296      */
    297     @Override
    298     public DrawableRequestBuilder<ModelType> animate(int animationId) {
    299         super.animate(animationId);
    300         return this;
    301     }
    302 
    303     /**
    304      * {@inheritDoc}
    305      */
    306     @Deprecated
    307     @SuppressWarnings("deprecation")
    308     @Override
    309     public DrawableRequestBuilder<ModelType> animate(Animation animation) {
    310         super.animate(animation);
    311         return this;
    312     }
    313 
    314     /**
    315      * {@inheritDoc}
    316      */
    317     @Override
    318     public DrawableRequestBuilder<ModelType> placeholder(int resourceId) {
    319         super.placeholder(resourceId);
    320         return this;
    321     }
    322 
    323     /**
    324      * {@inheritDoc}
    325      */
    326     @Override
    327     public DrawableRequestBuilder<ModelType> placeholder(Drawable drawable) {
    328         super.placeholder(drawable);
    329         return this;
    330     }
    331 
    332     /**
    333      * {@inheritDoc}
    334      */
    335     @Override
    336     public DrawableRequestBuilder<ModelType> error(int resourceId) {
    337         super.error(resourceId);
    338         return this;
    339     }
    340 
    341     /**
    342      * {@inheritDoc}
    343      */
    344     @Override
    345     public DrawableRequestBuilder<ModelType> error(Drawable drawable) {
    346         super.error(drawable);
    347         return this;
    348     }
    349 
    350     /**
    351      * {@inheritDoc}
    352      */
    353     @Override
    354     public DrawableRequestBuilder<ModelType> listener(
    355             RequestListener<? super ModelType, GlideDrawable> requestListener) {
    356         super.listener(requestListener);
    357         return this;
    358     }
    359 
    360     /**
    361      * {@inheritDoc}
    362      */
    363     @Override
    364     public DrawableRequestBuilder<ModelType> diskCacheStrategy(DiskCacheStrategy strategy) {
    365         super.diskCacheStrategy(strategy);
    366         return this;
    367     }
    368 
    369     /**
    370      * {@inheritDoc}
    371      */
    372     @Override
    373     public DrawableRequestBuilder<ModelType> skipMemoryCache(boolean skip) {
    374         super.skipMemoryCache(skip);
    375         return this;
    376     }
    377 
    378     /**
    379      * {@inheritDoc}
    380      */
    381     @Override
    382     public DrawableRequestBuilder<ModelType> override(int width, int height) {
    383         super.override(width, height);
    384         return this;
    385     }
    386 
    387     /**
    388      * {@inheritDoc}
    389      */
    390     @Override
    391     public DrawableRequestBuilder<ModelType> sourceEncoder(Encoder<ImageVideoWrapper> sourceEncoder) {
    392         super.sourceEncoder(sourceEncoder);
    393         return this;
    394     }
    395 
    396     /**
    397      * {@inheritDoc}
    398      */
    399     @Override
    400     public DrawableRequestBuilder<ModelType> dontTransform() {
    401         super.dontTransform();
    402         return this;
    403     }
    404 
    405     @Override
    406     public DrawableRequestBuilder<ModelType> signature(Key signature) {
    407         super.signature(signature);
    408         return this;
    409     }
    410 
    411     @Override
    412     public DrawableRequestBuilder<ModelType> load(ModelType model) {
    413         super.load(model);
    414         return this;
    415     }
    416 
    417     @Override
    418     public DrawableRequestBuilder<ModelType> clone() {
    419         return (DrawableRequestBuilder<ModelType>) super.clone();
    420     }
    421 
    422     /**
    423      * {@inheritDoc}
    424      *
    425      * <p>
    426      *     Note - If no transformation is set for this load, a default transformation will be applied based on the
    427      *     value returned from {@link android.widget.ImageView#getScaleType()}. To avoid this default transformation,
    428      *     use {@link #dontTransform()}.
    429      * </p>
    430      *
    431      * @param view {@inheritDoc}
    432      * @return {@inheritDoc}
    433      */
    434     @Override
    435     public Target<GlideDrawable> into(ImageView view) {
    436         return super.into(view);
    437     }
    438 
    439     @Override
    440     void applyFitCenter() {
    441         fitCenter();
    442     }
    443 
    444     @Override
    445     void applyCenterCrop() {
    446         centerCrop();
    447     }
    448 }
    449