Home | History | Annotate | Download | only in target
      1 package com.bumptech.glide.request.target;
      2 
      3 /**
      4  * A simple {@link com.bumptech.glide.request.target.Target} base class with default (usually noop) implementations
      5  * of non essential methods that allows the caller to specify an exact width/height. Typicaly use cases look something
      6  * like this:
      7  * <pre>
      8  * <code>
      9  * Glide.load("http://somefakeurl.com/fakeImage.jpeg")
     10  *      .asBitmap()
     11  *      .fitCenter()
     12  *      .into(new SimpleTarget<Bitmap>(250, 250) {
     13  *
     14  *          {@literal @Override}
     15  *          public void onResourceReady(Bitmap resource, GlideAnimation<Bitmap> glideAnimation) {
     16  *              // Do something with bitmap here.
     17  *          }
     18  *
     19  *      });
     20  * }
     21  * </code>
     22  * </pre>
     23  *
     24  * @param <Z> The type of resource that this target will receive.
     25  */
     26 public abstract class SimpleTarget<Z> extends BaseTarget<Z> {
     27     private final int width;
     28     private final int height;
     29 
     30     /**
     31      * Constructor for the target that assumes you will have called
     32      * {@link com.bumptech.glide.GenericRequestBuilder#override(int, int)} on the request builder this target is given
     33      * to.
     34      *
     35      * <p>
     36      *     Requests that load into this target will throw an {@link java.lang.IllegalArgumentException} if
     37      *     {@link com.bumptech.glide.GenericRequestBuilder#override(int, int)} was not called on the request builder.
     38      * </p>
     39      */
     40     public SimpleTarget() {
     41         this(-1, -1);
     42     }
     43 
     44     /**
     45      * Constructor for the target that takes the desired dimensions of the decoded and/or transformed resource.
     46      *
     47      * @param width The width in pixels of the desired resource.
     48      * @param height The height in pixels of the desired resource.
     49      */
     50     public SimpleTarget(int width, int height) {
     51         this.width = width;
     52         this.height = height;
     53     }
     54 
     55     /**
     56      * Immediately calls the given callback with the sizes given in the constructor.
     57      *
     58      * @param cb {@inheritDoc}
     59      */
     60     @Override
     61     public final void getSize(SizeReadyCallback cb) {
     62         if (width <= 0 || height <= 0) {
     63             throw new IllegalArgumentException("Width and height must both be > 0, but given width: " + width + " and"
     64                     + " height: " + height + ", either provide dimensions in the constructor or call override()");
     65         }
     66         cb.onSizeReady(width, height);
     67     }
     68 }
     69