Home | History | Annotate | Download | only in prefill
      1 package com.bumptech.glide.load.engine.prefill;
      2 
      3 import android.graphics.Bitmap;
      4 
      5 /**
      6  * A container for a set of options used to pre-fill a {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}
      7  * with {@link Bitmap Bitmaps} of a single size and configuration.
      8  */
      9 public final class PreFillType {
     10     // Visible for testing.
     11     static final Bitmap.Config DEFAULT_CONFIG = Bitmap.Config.RGB_565;
     12     private final int width;
     13     private final int height;
     14     private final Bitmap.Config config;
     15     private final int weight;
     16 
     17     /**
     18      * Constructor for a single type of {@link android.graphics.Bitmap}.
     19      *
     20      * @param width The width in pixels of the {@link android.graphics.Bitmap Bitmaps} to
     21      *              pre-fill.
     22      * @param height The height in pixels of the {@link android.graphics.Bitmap Bitmaps} to
     23      *               pre-fill.
     24      * @param config The {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap Bitmaps} to
     25      *               pre-fill.
     26      * @param weight An integer indicating how to balance pre-filling this size and configuration of
     27      * {@link android.graphics.Bitmap} against any other sizes/configurations that may be being pre-filled.
     28      */
     29     PreFillType(int width, int height, Bitmap.Config config, int weight) {
     30         if (config == null) {
     31             throw new NullPointerException("Config must not be null");
     32         }
     33 
     34         this.width = width;
     35         this.height = height;
     36         this.config = config;
     37         this.weight = weight;
     38     }
     39 
     40     /**
     41      * Returns the width in pixels of the {@link android.graphics.Bitmap Bitmaps}.
     42      */
     43     int getWidth() {
     44         return width;
     45     }
     46 
     47     /**
     48      * Returns the height in pixels of the {@link android.graphics.Bitmap Bitmaps}.
     49      */
     50     int getHeight() {
     51         return height;
     52     }
     53 
     54     /**
     55      * Returns the {@link android.graphics.Bitmap.Config} of the {@link android.graphics.Bitmap Bitmaps}.
     56      */
     57     Bitmap.Config getConfig() {
     58         return config;
     59     }
     60 
     61     /**
     62      * Returns the weight of the {@link android.graphics.Bitmap Bitmaps} of this type.
     63      */
     64     int getWeight() {
     65         return weight;
     66     }
     67 
     68     @Override
     69     public boolean equals(Object o) {
     70         if (o instanceof PreFillType) {
     71             PreFillType other = (PreFillType) o;
     72             return height == other.height
     73                     && width == other.width
     74                     && weight == other.weight
     75                     && config == other.config;
     76         }
     77         return false;
     78     }
     79 
     80     @Override
     81     public int hashCode() {
     82         int result = width;
     83         result = 31 * result + height;
     84         result = 31 * result + config.hashCode();
     85         result = 31 * result + weight;
     86         return result;
     87     }
     88 
     89     @Override
     90     public String toString() {
     91         return "PreFillSize{"
     92                 + "width=" + width
     93                 + ", height=" + height
     94                 + ", config=" + config
     95                 + ", weight=" + weight
     96                 + '}';
     97     }
     98 
     99     /**
    100      * Builder for {@link PreFillType}.
    101      */
    102     public static class Builder {
    103         private final int width;
    104         private final int height;
    105 
    106         private Bitmap.Config config;
    107         private int weight = 1;
    108 
    109         /**
    110          * Constructor for a builder that uses the given size as the width and height of the Bitmaps to prefill.
    111          * @param size The width and height in pixels of the Bitmaps to prefill.
    112          */
    113         public Builder(int size) {
    114             this(size, size);
    115         }
    116 
    117         /**
    118          * Constructor for a builder that uses the given dimensions as the dimensions of the Bitmaps to prefill.
    119          * @param width The width in pixels of the Bitmaps to prefill.
    120          * @param height The height in pixels of the Bitmaps to prefill.
    121          */
    122         public Builder(int width, int height) {
    123             if (width <= 0) {
    124                 throw new IllegalArgumentException("Width must be > 0");
    125             }
    126             if (height <= 0) {
    127                 throw new IllegalArgumentException("Height must be > 0");
    128             }
    129             this.width = width;
    130             this.height = height;
    131         }
    132 
    133         /**
    134          * Sets the {@link android.graphics.Bitmap.Config} for the Bitmaps to pre-fill.
    135          * @param config The config to use, or null to use Glide's default.
    136          * @return This builder.
    137          */
    138         public Builder setConfig(Bitmap.Config config) {
    139             this.config = config;
    140             return this;
    141         }
    142 
    143         /**
    144          * Returns the current {@link android.graphics.Bitmap.Config}.
    145          */
    146         Bitmap.Config getConfig() {
    147             return config;
    148         }
    149 
    150         /**
    151          * Sets the weight to use to balance how many Bitmaps of this type are prefilled relative to the other requested
    152          * types.
    153          * @param weight An integer indicating how to balance pre-filling this size and configuration of
    154          * {@link android.graphics.Bitmap} against any other sizes/configurations that may be being pre-filled.
    155          * @return This builder.
    156          */
    157         public Builder setWeight(int weight) {
    158             if (weight <= 0) {
    159                 throw new IllegalArgumentException("Weight must be > 0");
    160             }
    161             this.weight = weight;
    162             return this;
    163         }
    164 
    165         /**
    166          * Returns a new {@link PreFillType}.
    167          */
    168         PreFillType build() {
    169             return new PreFillType(width, height, config, weight);
    170         }
    171     }
    172 }
    173