Class LruBitmapPool
- java.lang.Object
-
- com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool
-
- All Implemented Interfaces:
BitmapPool
public class LruBitmapPool extends java.lang.Object implements BitmapPool
AnBitmapPoolimplementation that uses anLruPoolStrategyto bucketBitmaps and then uses an LRU eviction policy to evictBitmaps 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 voidclearMemory()Removes allBitmaps from the pool.longevictionCount()Returns the number of bitmaps that have been evicted from the pool.android.graphics.Bitmapget(int width, int height, android.graphics.Bitmap.Config config)Returns aBitmapof exactly the given width, height, and configuration, and containing only transparent pixels.longgetCurrentSize()Returns the current size of the pool in bytes.android.graphics.BitmapgetDirty(int width, int height, android.graphics.Bitmap.Config config)Identical toBitmapPool.get(int, int, android.graphics.Bitmap.Config)except that any returnedBitmapmay not have been erased and may contain random data.longgetMaxSize()Returns the current maximum size of the pool in bytes.longhitCount()Returns the number of cache hits for bitmaps in the pool.longmissCount()Returns the number of cache misses for bitmaps in the pool.voidput(android.graphics.Bitmap bitmap)Adds the givenBitmapif it is eligible to be re-used and the pool can fit it, or callsBitmap.recycle()on the Bitmap and discards it.voidsetSizeMultiplier(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.voidtrimMemory(int level)Reduces the size of the cache by evicting items based on the given level.
-
-
-
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 ofBitmap.Configthat 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:BitmapPoolReturns the current maximum size of the pool in bytes.- Specified by:
getMaxSizein interfaceBitmapPool
-
setSizeMultiplier
public void setSizeMultiplier(float sizeMultiplier)
Description copied from interface:BitmapPoolMultiplies 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:
setSizeMultiplierin interfaceBitmapPool- Parameters:
sizeMultiplier- The size multiplier to apply between 0 and 1.
-
put
public void put(android.graphics.Bitmap bitmap)
Description copied from interface:BitmapPoolAdds the givenBitmapif it is eligible to be re-used and the pool can fit it, or callsBitmap.recycle()on the Bitmap and discards it.Callers must not continue to use the Bitmap after calling this method.
- Specified by:
putin interfaceBitmapPool- Parameters:
bitmap- TheBitmapto 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:BitmapPoolReturns aBitmapof 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 thanBitmapPool.getDirty(int, int, android.graphics.Bitmap.Config). If theBitmapis being obtained to be used inBitmapFactoryor in any other case where every pixel in theBitmapwill 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:
getin interfaceBitmapPool- Parameters:
width- The width in pixels of the desiredBitmap.height- The height in pixels of the desiredBitmap.config- TheBitmap.Configof the desiredBitmap.- 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:BitmapPoolIdentical toBitmapPool.get(int, int, android.graphics.Bitmap.Config)except that any returnedBitmapmay 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 theBitmapentirely before writing new data to it.- Specified by:
getDirtyin interfaceBitmapPool- Parameters:
width- The width in pixels of the desiredBitmap.height- The height in pixels of the desiredBitmap.config- TheBitmap.Configof the desiredBitmap.- Returns:
- A
Bitmapwith 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:BitmapPoolRemoves allBitmaps from the pool.- Specified by:
clearMemoryin interfaceBitmapPool
-
trimMemory
public void trimMemory(int level)
Description copied from interface:BitmapPoolReduces the size of the cache by evicting items based on the given level.- Specified by:
trimMemoryin interfaceBitmapPool- Parameters:
level- The level fromComponentCallbacks2to use to determine how manyBitmaps to evict.- See Also:
ComponentCallbacks2
-
-