public class

ImageMemoryCache

extends Object
java.lang.Object
   ↳ com.pdftron.pdf.utils.ImageMemoryCache

Class Overview

This class handles image memory caching to avoid allocating/de-allocating bitmaps by reusing them

Summary

Public Methods
void addBitmapToCache(String key, BitmapDrawable bitmap)
Adds a new image to the memory and disk caches
void addBitmapToReusableSet(Bitmap bitmap)
Adds the specified bitmap to reusable set
void clearAll()
Clears all cached bitmaps including the reusable bitmap set.
void clearCache()
Clears caches (but still keeps the weak reference to the reusable bitmaps).
Bitmap decodeSampledBitmapFromDescriptor(FileDescriptor fileDescriptor, int reqWidth, int reqHeight)
Decode and sample down a bitmap from a file input stream to the requested width and height.
Bitmap decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight)
Decode and sample down a bitmap from a file to the requested width and height.
Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
Decode and sample down a bitmap from resources to the requested width and height.
Bitmap decodeSampledBitmapFromResource(Resources res, int resId, float downSampleFactor)
Decode and sample down a bitmap from resources to the requested width and height.
BitmapDrawable getBitmapFromCache(String key)
Returns bitmap having the specified key from cache
Bitmap getBitmapFromReusableSet(int width, int height, Bitmap.Config config)
Returns a bitmap with specified configuration from the reusable set
static ImageMemoryCache getInstance()
static void setDebug(boolean debug)
void setMemCacheSize(int memCacheSize)
Sets the memory cache size.
void setMemCacheSizePercent(float percent)
Sets the memory cache size based on a percentage of the max available VM memory.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public void addBitmapToCache (String key, BitmapDrawable bitmap)

Adds a new image to the memory and disk caches

Parameters
key The key used to store the image
bitmap The bitmap to cache

public void addBitmapToReusableSet (Bitmap bitmap)

Adds the specified bitmap to reusable set

Parameters
bitmap The bitmap

public void clearAll ()

Clears all cached bitmaps including the reusable bitmap set.

public void clearCache ()

Clears caches (but still keeps the weak reference to the reusable bitmaps).

public Bitmap decodeSampledBitmapFromDescriptor (FileDescriptor fileDescriptor, int reqWidth, int reqHeight)

Decode and sample down a bitmap from a file input stream to the requested width and height.

Parameters
fileDescriptor The file descriptor to read from
reqWidth The requested width of the resulting bitmap
reqHeight The requested height of the resulting bitmap
Returns
  • A bitmap sampled down from the original with the same aspect ratio and dimensions that are equal to or greater than the requested width and height

public Bitmap decodeSampledBitmapFromFile (String filename, int reqWidth, int reqHeight)

Decode and sample down a bitmap from a file to the requested width and height.

Parameters
filename The full path of the file to decode
reqWidth The requested width of the resulting bitmap, 0 for no down-sampling
reqHeight The requested height of the resulting bitmap, 0 for no down-sampling
Returns
  • A bitmap sampled down from the original with the same aspect ratio and dimensions that are equal to or greater than the requested width and height

public Bitmap decodeSampledBitmapFromResource (Resources res, int resId, int reqWidth, int reqHeight)

Decode and sample down a bitmap from resources to the requested width and height.

Parameters
res The resources object containing the image data
resId The resource id of the image data
reqWidth The requested width of the resulting bitmap, 0 for no down-sampling
reqHeight The requested height of the resulting bitmap, 0 for no down-sampling
Returns
  • A bitmap sampled down from the original with the same aspect ratio and dimensions that are equal to or greater than the requested width and height

public Bitmap decodeSampledBitmapFromResource (Resources res, int resId, float downSampleFactor)

Decode and sample down a bitmap from resources to the requested width and height.

Parameters
res The resources object containing the image data
resId The resource id of the image data
downSampleFactor The down-sampling factor, 1.0 for no down-sampling
Returns
  • A bitmap sampled down from the original with the same aspect ratio and dimensions that are equal to or greater than the requested width and height

public BitmapDrawable getBitmapFromCache (String key)

Returns bitmap having the specified key from cache

Parameters
key The bitmap key
Returns
  • The bitmap

public Bitmap getBitmapFromReusableSet (int width, int height, Bitmap.Config config)

Returns a bitmap with specified configuration from the reusable set

Parameters
width The expected width
height The expected height
config The bitmap configuration
Returns
  • The bitmap

public static ImageMemoryCache getInstance ()

public static void setDebug (boolean debug)

public void setMemCacheSize (int memCacheSize)

Sets the memory cache size.

Parameters
memCacheSize The memory cache size

public void setMemCacheSizePercent (float percent)

Sets the memory cache size based on a percentage of the max available VM memory. Eg. setting percent to 0.2 would set the memory cache to one fifth of the available memory. Throws IllegalArgumentException if percent is < 0.01 or > .8. memory cache size is stored in kilobytes instead of bytes as this will eventually be passed to construct a LruCache which takes an int in its constructor.

Parameters
percent Percent of available app memory to use to size memory cache