Class LruBitmapPool

  • All Implemented Interfaces:
    BitmapPool

    public class LruBitmapPool
    extends java.lang.Object
    implements BitmapPool
    An BitmapPool implementation that uses an LruPoolStrategy to bucket Bitmaps and then uses an LRU eviction policy to evict Bitmaps from the least recently used bucket in order to keep the pool below a given maximum size limit.
    • Constructor Summary

      Constructors 
      Constructor Description
      LruBitmapPool​(long maxSize)
      Constructor for LruBitmapPool.
      LruBitmapPool​(long maxSize, java.util.Set<android.graphics.Bitmap.Config> allowedConfigs)
      Constructor for LruBitmapPool.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clearMemory()
      Removes all Bitmaps from the pool.
      long evictionCount()
      Returns the number of bitmaps that have been evicted from the pool.
      android.graphics.Bitmap get​(int width, int height, android.graphics.Bitmap.Config config)
      Returns a Bitmap of exactly the given width, height, and configuration, and containing only transparent pixels.
      long getCurrentSize()
      Returns the current size of the pool in bytes.
      android.graphics.Bitmap getDirty​(int width, int height, android.graphics.Bitmap.Config config)
      Identical to BitmapPool.get(int, int, android.graphics.Bitmap.Config) except that any returned Bitmap may not have been erased and may contain random data.
      long getMaxSize()
      Returns the current maximum size of the pool in bytes.
      long hitCount()
      Returns the number of cache hits for bitmaps in the pool.
      long missCount()
      Returns the number of cache misses for bitmaps in the pool.
      void put​(android.graphics.Bitmap bitmap)
      Adds the given Bitmap if it is eligible to be re-used and the pool can fit it, or calls Bitmap.recycle() on the Bitmap and discards it.
      void setSizeMultiplier​(float sizeMultiplier)
      Multiplies the initial size of the pool by the given multiplier to dynamically and synchronously allow users to adjust the size of the pool.
      void trimMemory​(int level)
      Reduces the size of the cache by evicting items based on the given level.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LruBitmapPool

        public LruBitmapPool​(long maxSize)
        Constructor for LruBitmapPool.
        Parameters:
        maxSize - The initial maximum size of the pool in bytes.
      • LruBitmapPool

        public LruBitmapPool​(long maxSize,
                             java.util.Set<android.graphics.Bitmap.Config> allowedConfigs)
        Constructor for LruBitmapPool.
        Parameters:
        maxSize - The initial maximum size of the pool in bytes.
        allowedConfigs - A white listed put of Bitmap.Config that are allowed to be put into the pool. Configs not in the allowed put will be rejected.
    • Method Detail

      • hitCount

        public long hitCount()
        Returns the number of cache hits for bitmaps in the pool.
      • missCount

        public long missCount()
        Returns the number of cache misses for bitmaps in the pool.
      • evictionCount

        public long evictionCount()
        Returns the number of bitmaps that have been evicted from the pool.
      • getCurrentSize

        public long getCurrentSize()
        Returns the current size of the pool in bytes.
      • getMaxSize

        public long getMaxSize()
        Description copied from interface: BitmapPool
        Returns the current maximum size of the pool in bytes.
        Specified by:
        getMaxSize in interface BitmapPool
      • setSizeMultiplier

        public void setSizeMultiplier​(float sizeMultiplier)
        Description copied from interface: BitmapPool
        Multiplies the initial size of the pool by the given multiplier to dynamically and synchronously allow users to adjust the size of the pool.

        If the current total size of the pool is larger than the max size after the given multiplier is applied, Bitmaps should be evicted until the pool is smaller than the new max size.

        Specified by:
        setSizeMultiplier in interface BitmapPool
        Parameters:
        sizeMultiplier - The size multiplier to apply between 0 and 1.
      • put

        public void put​(android.graphics.Bitmap bitmap)
        Description copied from interface: BitmapPool
        Adds the given Bitmap if it is eligible to be re-used and the pool can fit it, or calls Bitmap.recycle() on the Bitmap and discards it.

        Callers must not continue to use the Bitmap after calling this method.

        Specified by:
        put in interface BitmapPool
        Parameters:
        bitmap - The Bitmap to attempt to add.
        See Also:
        Bitmap.isMutable(), Bitmap.recycle()
      • get

        @NonNull
        public android.graphics.Bitmap get​(int width,
                                           int height,
                                           android.graphics.Bitmap.Config config)
        Description copied from interface: BitmapPool
        Returns a Bitmap of exactly the given width, height, and configuration, and containing only transparent pixels.

        If no Bitmap with the requested attributes is present in the pool, a new one will be allocated.

        Because this method erases all pixels in the Bitmap, this method is slightly slower than BitmapPool.getDirty(int, int, android.graphics.Bitmap.Config). If the Bitmap is being obtained to be used in BitmapFactory or in any other case where every pixel in the Bitmap will always be overwritten or cleared, BitmapPool.getDirty(int, int, android.graphics.Bitmap.Config) will be faster. When in doubt, use this method to ensure correctness.

             Implementations can should clear out every returned Bitmap using the following:
        
         
         bitmap.eraseColor(Color.TRANSPARENT);
         
         
        Specified by:
        get in interface BitmapPool
        Parameters:
        width - The width in pixels of the desired Bitmap.
        height - The height in pixels of the desired Bitmap.
        config - The Bitmap.Config of the desired Bitmap.
        See Also:
        BitmapPool.getDirty(int, int, android.graphics.Bitmap.Config)
      • getDirty

        @NonNull
        public android.graphics.Bitmap getDirty​(int width,
                                                int height,
                                                android.graphics.Bitmap.Config config)
        Description copied from interface: BitmapPool
        Identical to BitmapPool.get(int, int, android.graphics.Bitmap.Config) except that any returned Bitmap may not have been erased and may contain random data.

        If no Bitmap with the requested attributes is present in the pool, a new one will be allocated.

        Although this method is slightly more efficient than BitmapPool.get(int, int, android.graphics.Bitmap.Config) it should be used with caution and only when the caller is sure that they are going to erase the Bitmap entirely before writing new data to it.

        Specified by:
        getDirty in interface BitmapPool
        Parameters:
        width - The width in pixels of the desired Bitmap.
        height - The height in pixels of the desired Bitmap.
        config - The Bitmap.Config of the desired Bitmap.
        Returns:
        A Bitmap with exactly the given width, height, and config potentially containing random image data.
        See Also:
        BitmapPool.get(int, int, android.graphics.Bitmap.Config)
      • clearMemory

        public void clearMemory()
        Description copied from interface: BitmapPool
        Removes all Bitmaps from the pool.
        Specified by:
        clearMemory in interface BitmapPool
      • trimMemory

        public void trimMemory​(int level)
        Description copied from interface: BitmapPool
        Reduces the size of the cache by evicting items based on the given level.
        Specified by:
        trimMemory in interface BitmapPool
        Parameters:
        level - The level from ComponentCallbacks2 to use to determine how many Bitmaps to evict.
        See Also:
        ComponentCallbacks2