Android Open Source - Android-Advanced-ImageView Bitmap Cache






From Project

Back to project page Android-Advanced-ImageView.

License

The source code is released under:

GNU General Public License

If you think the Android project Android-Advanced-ImageView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.appsdk.advancedimageview.util;
//from ww w. j ava 2 s .  com
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;

public class BitmapCache {
  public static final String TAG = "BitmapCache";
  private static final boolean LOG_ENABLED = false;

  private static BitmapCache mInstance;
  private final MyLruCache<String, Bitmap> mMemCache;

  public static BitmapCache getInstance(Context context) {
    if (mInstance == null)
      mInstance = new BitmapCache(context);
    return mInstance;
  }

  private BitmapCache(Context context) {
    // Get memory class of this device, exceeding this amount will throw an
    // OutOfMemory exception.
    final int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
    // Use 1/5th of the available memory for this memory cache.
    final int cacheSize = 1024 * 1024 * memClass / 5;

    if (LOG_ENABLED)
      Log.d(TAG, "LRUCache size sets to " + cacheSize);

    mMemCache = new MyLruCache<String, Bitmap>(cacheSize) {
      @Override
      protected int sizeOf(String key, Bitmap value) {
        return value.getRowBytes() * value.getHeight();
      }
    };
  }

  public Bitmap getBitmapFromMemCache(String key) {
    final Bitmap b = mMemCache.get(key);
    if (LOG_ENABLED)
      Log.d(TAG, (b == null) ? "Cache miss" : "Cache found");
    if (b != null && b.isRecycled()) {
      /* A recycled bitmap cannot be used again */
      mMemCache.remove(key);
      return null;
    }
    return b;
  }

  public void addBitmapToMemCache(String key, Bitmap bitmap) {
    if (key != null && bitmap != null && getBitmapFromMemCache(key) == null)
      mMemCache.put(key, bitmap);
  }
  
  public void removeBitmapFromMemCache(String key) {
    mMemCache.remove(key);
  }

  private Bitmap getBitmapFromMemCache(int resId) {
    return getBitmapFromMemCache("res:" + resId);
  }

  private void addBitmapToMemCache(int resId, Bitmap bitmap) {
    addBitmapToMemCache("res:" + resId, bitmap);
  }

  public static Bitmap GetFromResource(Context context, View v, int resId) {
    BitmapCache cache = BitmapCache.getInstance(context);
    Bitmap bitmap = cache.getBitmapFromMemCache(resId);
    if (bitmap == null) {
      bitmap = BitmapFactory.decodeResource(v.getResources(), resId);
      cache.addBitmapToMemCache(resId, bitmap);
    }
    return bitmap;
  }

  public void clear() {
    mMemCache.evictAll();
  }

  public void destroy() {
    clear();
    mInstance = null;
  }
}




Java Source Code List

com.appsdk.advancedimageview.AdvancedImageCarouselViewGroup.java
com.appsdk.advancedimageview.AdvancedImageCarousel.java
com.appsdk.advancedimageview.AdvancedImageView.java
com.appsdk.advancedimageview.listener.AdvancedImageCarouselClickListener.java
com.appsdk.advancedimageview.listener.AdvancedImageCarouselSwitchListener.java
com.appsdk.advancedimageview.listener.AdvancedImageViewLoadListener.java
com.appsdk.advancedimageview.util.AssetsManager.java
com.appsdk.advancedimageview.util.AsyncLocalImageLoader.java
com.appsdk.advancedimageview.util.AsyncNetImageLoader.java
com.appsdk.advancedimageview.util.BitmapCache.java
com.appsdk.advancedimageview.util.DataCleanManager.java
com.appsdk.advancedimageview.util.MD5.java
com.appsdk.advancedimageview.util.MessageDigest.java
com.appsdk.advancedimageview.util.MyLruCache.java
com.appsdk.advancedimageview.util.mConfig.java