fix(cache-images): respect decodeWidth and decodeHeight when caching images (#115)

* feat(image-fetcher): limit bitmap to device sizes when no request width/height set.

* fix(cache-images): create new cache for images with different decodeHeight/decodeWidth.
This commit is contained in:
Alexander Djenkov
2018-03-15 16:39:01 +02:00
committed by Manol Donev
parent 9d2095cdad
commit c2cd53fe5f
2 changed files with 30 additions and 8 deletions

View File

@@ -48,6 +48,9 @@ public class Fetcher extends Worker {
private static final String HTTP_CACHE_DIR = "http"; private static final String HTTP_CACHE_DIR = "http";
private static final int IO_BUFFER_SIZE = 8 * 1024; private static final int IO_BUFFER_SIZE = 8 * 1024;
private static int mDeviceWidthPixels;
private static int mDeviceHeightPixels;
private File mHttpCacheDir; private File mHttpCacheDir;
private DiskLruCache mHttpDiskCache; private DiskLruCache mHttpDiskCache;
private boolean mHttpDiskCacheStarting = true; private boolean mHttpDiskCacheStarting = true;
@@ -74,6 +77,8 @@ public class Fetcher extends Worker {
super(context); super(context);
mHttpCacheDir = Cache.getDiskCacheDir(context, HTTP_CACHE_DIR); mHttpCacheDir = Cache.getDiskCacheDir(context, HTTP_CACHE_DIR);
mPackageName = context.getPackageName(); mPackageName = context.getPackageName();
mDeviceWidthPixels = (int) context.getResources().getDisplayMetrics().widthPixels;
mDeviceHeightPixels = (int) context.getResources().getDisplayMetrics().heightPixels;
} }
@Override @Override
@@ -477,15 +482,15 @@ public class Fetcher extends Worker {
int sourceWidth = bitmap.getWidth(); int sourceWidth = bitmap.getWidth();
int sourceHeight = bitmap.getHeight(); int sourceHeight = bitmap.getHeight();
reqWidth = reqWidth > 0 ? reqWidth : sourceWidth; reqWidth = reqWidth > 0 ? reqWidth : Math.min(sourceWidth, mDeviceWidthPixels);
reqHeight = reqHeight > 0 ? reqHeight : sourceHeight; reqHeight = reqHeight > 0 ? reqHeight : Math.min(sourceHeight, mDeviceHeightPixels);
// scale // scale
if (reqWidth != sourceWidth || reqHeight != sourceHeight) { if (reqWidth != sourceWidth || reqHeight != sourceHeight) {
if (keepAspectRatio) { if (keepAspectRatio) {
double widthCoef = (double) sourceWidth / (double) reqWidth; double widthCoef = (double) sourceWidth / (double) reqWidth;
double heightCoef = (double) sourceHeight / (double) reqHeight; double heightCoef = (double) sourceHeight / (double) reqHeight;
double aspectCoef = widthCoef > heightCoef ? widthCoef : heightCoef; double aspectCoef = Math.min(widthCoef, heightCoef);
reqWidth = (int) Math.floor(sourceWidth / aspectCoef); reqWidth = (int) Math.floor(sourceWidth / aspectCoef);
reqHeight = (int) Math.floor(sourceHeight / aspectCoef); reqHeight = (int) Math.floor(sourceHeight / aspectCoef);

View File

@@ -101,12 +101,17 @@ public abstract class Worker {
} }
Bitmap value = null; Bitmap value = null;
String cacheUri = uri;
if (debuggable > 0) { if (debuggable > 0) {
Log.v(TAG, "loadImage on: " + owner + " to: " + uri); Log.v(TAG, "loadImage on: " + owner + " to: " + uri);
} }
if (mCache != null && useCache) { if (mCache != null && useCache) {
value = mCache.getBitmapFromMemCache(uri); // Create new image cache for images with different decodeHeight/decodeWidth.
cacheUri = createCacheUri(uri, decodeHeight, decodeWidth);
value = mCache.getBitmapFromMemCache(cacheUri);
} }
if (value == null && !async) { if (value == null && !async) {
@@ -115,9 +120,9 @@ public abstract class Worker {
if (value != null) { if (value != null) {
if (mCache != null && useCache) { if (mCache != null && useCache) {
if (debuggable > 0) { if (debuggable > 0) {
Log.v(TAG, "loadImage.addBitmapToCache: " + owner + ", src: " + uri); Log.v(TAG, "loadImage.addBitmapToCache: " + owner + ", src: " + cacheUri);
} }
mCache.addBitmapToCache(uri, value); mCache.addBitmapToCache(cacheUri, value);
} }
} }
} }
@@ -257,6 +262,16 @@ public abstract class Worker {
return null; return null;
} }
/**
* Create cache key depending on image uri and decode properties.
*/
private static String createCacheUri(String uri, int decodeHeight, int decodeWidth) {
uri += decodeHeight != 0 ? "height%%" + String.valueOf(decodeHeight): "";
uri += decodeWidth != 0 ? "width%%" + String.valueOf(decodeWidth): "";
return uri;
}
/** /**
* The actual AsyncTask that will asynchronously process the image. * The actual AsyncTask that will asynchronously process the image.
*/ */
@@ -265,6 +280,7 @@ public abstract class Worker {
private int mDecodeHeight; private int mDecodeHeight;
private boolean mKeepAspectRatio; private boolean mKeepAspectRatio;
private String mUri; private String mUri;
private String mCacheUri;
private boolean mCacheImage; private boolean mCacheImage;
private final WeakReference<BitmapOwner> imageViewReference; private final WeakReference<BitmapOwner> imageViewReference;
private final OnImageLoadedListener mOnImageLoadedListener; private final OnImageLoadedListener mOnImageLoadedListener;
@@ -279,6 +295,7 @@ public abstract class Worker {
mKeepAspectRatio = keepAspectRatio; mKeepAspectRatio = keepAspectRatio;
mCacheImage = cacheImage; mCacheImage = cacheImage;
mUri = uri; mUri = uri;
mCacheUri = createCacheUri(uri, decodeHeight, decodeWidth);
imageViewReference = new WeakReference<BitmapOwner>(owner); imageViewReference = new WeakReference<BitmapOwner>(owner);
mOnImageLoadedListener = listener; mOnImageLoadedListener = listener;
} }
@@ -320,9 +337,9 @@ public abstract class Worker {
if (bitmap != null) { if (bitmap != null) {
if (mCache != null && mCacheImage) { if (mCache != null && mCacheImage) {
if (debuggable > 0) { if (debuggable > 0) {
Log.v(TAG, "addBitmapToCache: " + imageViewReference.get() + ", src: " + mUri); Log.v(TAG, "addBitmapToCache: " + imageViewReference.get() + ", src: " + mCacheUri);
} }
mCache.addBitmapToCache(mUri, bitmap); mCache.addBitmapToCache(mCacheUri, bitmap);
} }
} }