mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-08-31 21:41:33 +00:00
#1458 Re-enabled image memory caching
This commit is contained in:
parent
4a003d7678
commit
a0ef89604b
2 changed files with 13 additions and 8 deletions
|
@ -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);
|
||||||
|
|
|
@ -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,8 +31,9 @@ public class MemoryCache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void put(String id, Bitmap bitmap) {
|
public void put(String url, Bitmap bitmap, boolean hasRoundCorners) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
String id = getPhotoCacheId(url, hasRoundCorners);
|
||||||
if (cache.containsKey(id)) {
|
if (cache.containsKey(id)) {
|
||||||
size -= getSizeInBytes(cache.get(id));
|
size -= getSizeInBytes(cache.get(id));
|
||||||
}
|
}
|
||||||
|
@ -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();
|
||||||
|
|
Loading…
Add table
Reference in a new issue