#1458 Re-enabled image memory caching

This commit is contained in:
sictiru 2021-06-10 00:43:59 -07:00
parent 4a003d7678
commit a0ef89604b
2 changed files with 13 additions and 8 deletions

View file

@ -85,7 +85,7 @@ public class ImageLoader {
url = buildUrlIfNeeded(url); url = buildUrlIfNeeded(url);
// try from memory // try from memory
Bitmap bitmap = memoryCache.get(url); Bitmap bitmap = memoryCache.get(url, false);
if (bitmap != null) { if (bitmap != null) {
remoteViews.setImageViewBitmap(imageViewId, bitmap); remoteViews.setImageViewBitmap(imageViewId, bitmap);
remoteViews.setViewVisibility(imageViewId, View.VISIBLE); remoteViews.setViewVisibility(imageViewId, View.VISIBLE);
@ -100,7 +100,7 @@ public class ImageLoader {
} }
if (bitmap != null) { if (bitmap != null) {
memoryCache.put(url, bitmap); memoryCache.put(url, bitmap, false);
remoteViews.setImageViewBitmap(imageViewId, bitmap); remoteViews.setImageViewBitmap(imageViewId, bitmap);
remoteViews.setViewVisibility(imageViewId, View.VISIBLE); remoteViews.setViewVisibility(imageViewId, View.VISIBLE);
} else { } else {
@ -155,8 +155,7 @@ public class ImageLoader {
if (photoToLoad.cancel) return; if (photoToLoad.cancel) return;
// try from memory // try from memory
//TODO: revisit memory cache Bitmap bitmap = memoryCache.get(photoToLoad.url, photoToLoad.roundCorners);
Bitmap bitmap = null;
if (bitmap != null) { if (bitmap != null) {
setViewImage(bitmap, photoToLoad); setViewImage(bitmap, photoToLoad);
@ -194,7 +193,7 @@ public class ImageLoader {
} }
if (bitmap != null) { if (bitmap != null) {
memoryCache.put(photoToLoad.url, bitmap); memoryCache.put(photoToLoad.url, bitmap, photoToLoad.roundCorners);
} }
if (photoToLoad.cancel) return; if (photoToLoad.cancel) return;
setViewImage(bitmap, photoToLoad); setViewImage(bitmap, photoToLoad);

View file

@ -18,8 +18,9 @@ public class MemoryCache {
this.limit = limitBytes; this.limit = limitBytes;
} }
public Bitmap get(String id){ public Bitmap get(String url, boolean hasRoundCorners){
try { try {
String id = getPhotoCacheId(url, hasRoundCorners);
if (cache == null || !cache.containsKey(id)) { if (cache == null || !cache.containsKey(id)) {
return null; return null;
} else { } else {
@ -30,9 +31,10 @@ public class MemoryCache {
} }
} }
public void put(String id, Bitmap bitmap) { public void put(String url, Bitmap bitmap, boolean hasRoundCorners) {
synchronized (this) { synchronized (this) {
if (cache.containsKey(id)) { String id = getPhotoCacheId(url, hasRoundCorners);
if (cache.containsKey(id)) {
size -= getSizeInBytes(cache.get(id)); size -= getSizeInBytes(cache.get(id));
} }
cache.put(id, bitmap); cache.put(id, bitmap);
@ -41,6 +43,10 @@ public class MemoryCache {
} }
} }
public String getPhotoCacheId(String url, boolean hasRoundCorners) {
return hasRoundCorners ? url + "-rounded" : url;
}
private void checkSize() { private void checkSize() {
if (size > limit) { if (size > limit) {
final Iterator<Entry<String, Bitmap>> iter = cache.entrySet().iterator(); final Iterator<Entry<String, Bitmap>> iter = cache.entrySet().iterator();