-
- All Implemented Interfaces:
-
com.mobwith.imgmodule.load.engine.bitmap_recycle.BitmapPool
public class LruBitmapPool implements BitmapPool
An com.mobwith.imgmodule.load.engine.bitmap_recycle.BitmapPool implementation that uses an com.mobwith.imgmodule.load.engine.bitmap_recycle.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.
-
-
Field Summary
Fields Modifier and Type Field Description private longmaxSizeprivate longcurrentSize
-
Constructor Summary
Constructors Constructor Description LruBitmapPool(long maxSize)Constructor for LruBitmapPool. LruBitmapPool(long maxSize, Set<Bitmap.Config> allowedConfigs)Constructor for LruBitmapPool.
-
Method Summary
Modifier and Type Method Description longgetMaxSize()Returns the current maximum size of the pool in bytes. longgetCurrentSize()Returns the current 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. longevictionCount()Returns the number of bitmaps that have been evicted from the pool. synchronized voidsetSizeMultiplier(float sizeMultiplier)Multiplies the initial size of the pool by the given multiplier to dynamically andsynchronously allow users to adjust the size of the pool. synchronized voidput(Bitmap bitmap)Adds the given Bitmap if it is eligible to be re-used and the pool canfit it, or calls recycle on the Bitmap and discards it. Bitmapget(int width, int height, Bitmap.Config config)Returns a Bitmap of exactly the given width, height, andconfiguration, and containing only transparent pixels. BitmapgetDirty(int width, int height, Bitmap.Config config)Identical to get except that any returned Bitmap may not have been erased and may contain random data. voidclearMemory()Removes all Bitmaps from the pool. voidtrimMemory(int level)Reduces the size of the cache by evicting items based on the given level. -
-
Constructor Detail
-
LruBitmapPool
LruBitmapPool(long maxSize)
Constructor for LruBitmapPool.- Parameters:
maxSize- The initial maximum size of the pool in bytes.
-
LruBitmapPool
LruBitmapPool(long maxSize, Set<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 areallowed to be put into the pool.
-
-
Method Detail
-
getMaxSize
long getMaxSize()
Returns the current maximum size of the pool in bytes.
-
getCurrentSize
long getCurrentSize()
Returns the current size of the pool in bytes.
-
hitCount
long hitCount()
Returns the number of cache hits for bitmaps in the pool.
-
missCount
long missCount()
Returns the number of cache misses for bitmaps in the pool.
-
evictionCount
long evictionCount()
Returns the number of bitmaps that have been evicted from the pool.
-
setSizeMultiplier
synchronized void setSizeMultiplier(float sizeMultiplier)
Multiplies the initial size of the pool by the given multiplier to dynamically andsynchronously 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 multiplieris applied, Bitmaps should be evicted until the pool is smaller than the new max size.
- Parameters:
sizeMultiplier- The size multiplier to apply between 0 and 1.
-
put
synchronized void put(Bitmap bitmap)
Adds the given Bitmap if it is eligible to be re-used and the pool canfit it, or calls recycle on the Bitmap and discards it.
Callers must not continue to use the Bitmap after calling this method.
- Parameters:
bitmap- The Bitmap to attempt to add.
-
get
@NonNull() Bitmap get(int width, int height, Bitmap.Config config)
Returns a Bitmap of exactly the given width, height, andconfiguration, and containing only transparent pixels.
If no Bitmap with the requested attributes is present in the pool, a new one will beallocated.
Because this method erases all pixels in the Bitmap, this method is slightly slowerthan getDirty. If the is being obtained to be used in android.graphics.BitmapFactory or in any other case where every pixel in the Bitmap will always beoverwritten or cleared, getDirty will befaster. When in doubt, use this method to ensure correctness.
Implementations can should clear out every returned Bitmap using the following:{@code * bitmap.eraseColor(Color.TRANSPARENT); * }- 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 .
-
getDirty
@NonNull() Bitmap getDirty(int width, int height, Bitmap.Config config)
Identical to get 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 beallocated.
Although this method is slightly more efficient than get it should be used with caution and only when the caller issure that they are going to erase the Bitmap entirely before writingnew data to it.
- 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 .
-
clearMemory
void clearMemory()
Removes all Bitmaps from the pool.
-
trimMemory
void trimMemory(int level)
Reduces the size of the cache by evicting items based on the given level.
- Parameters:
level- The level from android.content.ComponentCallbacks2 to use to determine howmany Bitmaps to evict.
-
-
-
-