Back to project page ImageScanner.
The source code is released under:
Apache License
If you think the Android project ImageScanner listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.scanner.utils; //w ww . j a va2 s.c o m import java.util.HashSet; import java.util.Set; import android.content.Context; import android.graphics.Bitmap; import android.os.AsyncTask; import android.util.LruCache; import android.view.View; import android.widget.ImageView; /** * ImageLoaderCacheHelper * * @author Quinn * */ public class ImageLoaderCacheHelper { public interface SetImageCallBack { public void setImage(ImageView imageView, Bitmap bitmap); } private final static String TAG = "ImageLoaderCacheHelper"; private LruCache<String, Bitmap> mMemoryCache; private Set<BitmapWorkerTask> taskCollection; private static Context context; private static ImageLoaderCacheHelper instance = null; public static ImageLoaderCacheHelper getInstance(Context context) { synchronized (ImageLoaderCacheHelper.class) { if (instance == null) { instance = new ImageLoaderCacheHelper(context); ImageLoaderCacheHelper.context = context; } } return instance; } private ImageLoaderCacheHelper(Context context) { taskCollection = new HashSet<BitmapWorkerTask>(); int maxMemory = (int) Runtime.getRuntime().maxMemory(); int cacheSize = maxMemory / 8; mMemoryCache = new LruCache<String, Bitmap>(cacheSize) { @Override protected int sizeOf(String key, Bitmap bitmap) { return bitmap.getByteCount(); } }; } public void addBitmapToMemoryCache(String key, Bitmap bitmap) { if (getBitmapFromMemoryCache(key) == null) { mMemoryCache.put(key, bitmap); } } public Bitmap getBitmapFromMemoryCache(String key) { return mMemoryCache.get(key); } public void loadBitmaps(ImageView imageView, String imageUrl, View defaultView, SetImageCallBack callback) { try { Bitmap bitmap = getBitmapFromMemoryCache(imageUrl); if (bitmap == null) { /** * if bitmap is null, load it with a task; */ BitmapWorkerTask task = new BitmapWorkerTask(imageView, defaultView, callback); taskCollection.add(task); task.execute(imageUrl); } else { if (imageView != null && bitmap != null) { callback.setImage(imageView, bitmap); crossfade(defaultView, imageView); } } } catch (Exception e) { e.printStackTrace(); } } /** * The task to load a image * * @author Quinn */ class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> { private ImageView imageView; private String imageUrl; private SetImageCallBack callback; private View defaultView; public BitmapWorkerTask(ImageView imageView, View defaultView, SetImageCallBack callback) { this.imageView = imageView; this.callback = callback; this.defaultView = defaultView; } @Override protected Bitmap doInBackground(String... params) { imageUrl = params[0]; Bitmap bitmap = loadBitmap(imageUrl); addBitmapToMemoryCache(imageUrl, bitmap); return bitmap; } @Override protected void onPostExecute(Bitmap bitmap) { super.onPostExecute(bitmap); if (imageView != null && bitmap != null) { callback.setImage(imageView, bitmap); crossfade(defaultView, imageView); } taskCollection.remove(this); } private Bitmap loadBitmap(String imageUrl) { int rectSize = DensityUtil.getScreenHeight(context) / 16; Bitmap bitmap0 = BitmapUtils.decodeBitmapFromFile(imageUrl, rectSize , rectSize); Bitmap bitmap1 = BitmapUtils.centerSquareScaleBitmap(bitmap0, rectSize); return bitmap1; } } private void crossfade(final View currView, View nextView) { if (nextView != null) { nextView.setVisibility(View.VISIBLE); } if (currView != null) { currView.setVisibility(View.GONE); } } }