-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add our own version of an lru memory cache
Allows us to remove our dependency on the android support libs.
- Loading branch information
Sam Judd
committed
Aug 8, 2013
1 parent
0d133b4
commit 76c7bc1
Showing
4 changed files
with
65 additions
and
57 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
library/src/com/bumptech/glide/resize/cache/LruMemoryCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2012. Bump Technologies Inc. All Rights Reserved. | ||
*/ | ||
|
||
package com.bumptech.glide.resize.cache; | ||
|
||
import android.graphics.Bitmap; | ||
|
||
import java.util.LinkedHashMap; | ||
|
||
/** | ||
*/ | ||
public class LruMemoryCache implements MemoryCache { | ||
private final LinkedHashMap<String, Bitmap> cache = new LinkedHashMap<String, Bitmap>(15, 0.75f, true); | ||
private final int maxSize; | ||
private ImageRemovedListener imageRemovedListener; | ||
private int currentSize = 0; | ||
|
||
private static int getSize(Bitmap bitmap) { | ||
return bitmap.getHeight() * bitmap.getRowBytes(); | ||
} | ||
|
||
public LruMemoryCache(int size) { | ||
this.maxSize = size; | ||
} | ||
|
||
@Override | ||
public boolean contains(String key) { | ||
return cache.get(key) != null; | ||
} | ||
|
||
@Override | ||
public Bitmap get(String key) { | ||
return cache.get(key); | ||
} | ||
|
||
@Override | ||
public Bitmap put(String key, Bitmap bitmap) { | ||
currentSize += getSize(bitmap); | ||
final Bitmap result = cache.put(key, bitmap); | ||
evict(); | ||
return result; | ||
} | ||
|
||
@Override | ||
public void setImageRemovedListener(ImageRemovedListener listener) { | ||
this.imageRemovedListener = listener; | ||
} | ||
|
||
private void evict() { | ||
LinkedHashMap.Entry<String, Bitmap> last; | ||
while (currentSize > maxSize) { | ||
last = cache.entrySet().iterator().next(); | ||
final Bitmap toRemove = last.getValue(); | ||
currentSize -= getSize(toRemove); | ||
cache.remove(last.getKey()); | ||
|
||
if (imageRemovedListener != null) { | ||
imageRemovedListener.onImageRemoved(toRemove); | ||
} | ||
} | ||
} | ||
} |
55 changes: 0 additions & 55 deletions
55
library/src/com/bumptech/glide/resize/cache/LruPhotoCache.java
This file was deleted.
Oops, something went wrong.