Clean up image loading code, and switch to using Fresco.

This commit is contained in:
Dmitry Brant 2017-05-19 15:08:05 +02:00
parent 1a1fc143c9
commit bb49fb9801
7 changed files with 68 additions and 254 deletions

View file

@ -6,14 +6,13 @@ import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Application;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v4.util.LruCache;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.stetho.Stetho;
import com.nostra13.universalimageloader.cache.disc.impl.TotalSizeLimitedDiscCache;
import com.nostra13.universalimageloader.core.ImageLoader;
@ -112,31 +111,11 @@ public class CommonsApplication extends Application {
.build();
ImageLoader.getInstance().init(imageLoaderConfiguration);
Fresco.initialize(this);
// Initialize EventLogging
EventLog.setApp(this);
// based off https://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
// Cache for 1/8th of available VM memory
long maxMem = Runtime.getRuntime().maxMemory();
if (maxMem < 48L * 1024L * 1024L) {
// Cache only one bitmap if VM memory is too small (such as Nexus One);
Timber.d("Skipping bitmap cache; max mem is: %d", maxMem);
imageCache = new LruCache<>(1);
} else {
int cacheSize = (int) (maxMem / (1024 * 8));
Timber.d("Bitmap cache size %d from max mem %d", cacheSize, maxMem);
imageCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
int bitmapSize;
bitmapSize = bitmap.getByteCount();
// The cache size will be measured in kilobytes rather than number of items.
return bitmapSize / 1024;
}
};
}
//For caching area -> categories
cacheData = new CacheController();
@ -145,27 +124,6 @@ public class CommonsApplication extends Application {
volleyQueue.start();
}
private com.android.volley.toolbox.ImageLoader imageLoader;
private LruCache<String, Bitmap> imageCache;
public com.android.volley.toolbox.ImageLoader getImageLoader() {
if(imageLoader == null) {
imageLoader = new com.android.volley.toolbox.ImageLoader(volleyQueue, new com.android.volley.toolbox.ImageLoader.ImageCache() {
@Override
public Bitmap getBitmap(String key) {
return imageCache.get(key);
}
@Override
public void putBitmap(String key, Bitmap bitmap) {
imageCache.put(key, bitmap);
}
});
imageLoader.setBatchedResponseDelay(0);
}
return imageLoader;
}
public MWApi getApi() {
return api;
}