UnrefedBitmapCachepublic class UnrefedBitmapCache extends UnrefedPooledCache implements BitmapCacheThis subclass provides custom pool behavior. The pool can be set to block on {@link #poll()} if
nothing can be returned. This is useful if you know you will incur high costs upon receiving
nothing from the pool, and you do not want to incur those costs at the critical moment when the
UI is animating.
This subclass provides custom cache behavior. Null values can be cached. Later,
when the same key is used to retrieve the value, a {@link NullReusableBitmap} singleton will
be returned. |
Fields Summary |
---|
private boolean | mBlocking | private final Object | mLock | private android.util.LruCache | mNullRequests | private static final boolean | DEBUG | private static final String | TAG |
Constructors Summary |
---|
public UnrefedBitmapCache(int targetSizeBytes, float nonPooledFraction, int nullCapacity)
super(targetSizeBytes, nonPooledFraction);
if (nullCapacity > 0) {
mNullRequests = new LruCache<RequestKey, NullReusableBitmap>(nullCapacity);
}
|
Methods Summary |
---|
public ReusableBitmap | get(RequestKey key, boolean incrementRefCount)
if (mNullRequests != null && mNullRequests.get(key) != null) {
return NullReusableBitmap.getInstance();
}
return super.get(key, incrementRefCount);
| public void | offer(ReusableBitmap value)
synchronized (mLock) {
super.offer(value);
if (DEBUG) {
Log.d(TAG, "AltBitmapCache: offer +1");
}
// new resource gained. Notify one thread.
mLock.notify();
}
| public ReusableBitmap | poll()If {@link #setBlocking(boolean)} has been called with true, this method will block until a
resource is available.
ReusableBitmap bitmap;
synchronized (mLock) {
while ((bitmap = super.poll()) == null && mBlocking) {
if (DEBUG) {
Log.d(TAG, String.format(
"AltBitmapCache: %s waiting", Thread.currentThread().getName()));
}
Trace.beginSection("sleep");
try {
// block
mLock.wait();
if (DEBUG) {
Log.d(TAG, String.format("AltBitmapCache: %s notified",
Thread.currentThread().getName()));
}
} catch (InterruptedException ignored) {
}
Trace.endSection();
}
}
return bitmap;
| public ReusableBitmap | put(RequestKey key, ReusableBitmap value)Note: The cache only supports same-sized bitmaps.
if (mNullRequests != null && (value == null || value == NullReusableBitmap.getInstance())) {
mNullRequests.put(key, NullReusableBitmap.getInstance());
return null;
}
return super.put(key, value);
| public void | setBlocking(boolean blocking)Declare that {@link #poll()} should now block until it can return something.
synchronized (mLock) {
if (DEBUG) {
Log.d(TAG, String.format("AltBitmapCache: block %b", blocking));
}
mBlocking = blocking;
if (!mBlocking) {
// no longer blocking. Notify every thread.
mLock.notifyAll();
}
}
| protected int | sizeOf(ReusableBitmap value)
return value.getByteCount();
|
|