Android Open Source - zionhs Bitmap Helper






From Project

Back to project page zionhs.

License

The source code is released under:

GNU General Public License

If you think the Android project zionhs 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.ctc.android.widget;
/*  w  w  w.  j a v a2 s  .  c  om*/
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.Log;

/**
 * This class helps caching images for faster loading on second activity open.
 * May also be used at startup to load all images into memory.
 * Created by fess on 2/6/14.
 *
 * The implementation is taken from the official manual:
 * @link http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
 *
 * TODO: implement asynchronous image loading.
 * TODO: configurable cache size.
 *
 */
public class BitmapHelper
{
  private LruCache<String, Bitmap> mMemoryCache;

  public static BitmapHelper instance;

  public static BitmapHelper getInstance()
  {
    if (null == instance)
    {
      instance = new BitmapHelper();
    }
    return instance;
  }

  private BitmapHelper()
  {
    //trying to implement image caching
    // Get max available VM memory, exceeding this amount will throw an
    // OutOfMemory exception. Stored in kilobytes as LruCache takes an
    // int in its constructor.
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // Use 1/2nd of the available memory for this memory cache.
    final int cacheSize = maxMemory / 2;

    mMemoryCache = new LruCache<String, Bitmap>(cacheSize)
    {
      @Override
      protected int sizeOf(String key, Bitmap bitmap)
      {
        // The cache size will be measured in kilobytes rather than
        // number of items.
        return bitmap.getRowBytes()*bitmap.getHeight() / 1024;
      }
    };
  }

  public void addBitmapToMemoryCache(String key, Bitmap bitmap)
  {
    if (getBitmapFromMemCache(key) == null)
    {
      Log.e("Bitmap Helper", "Putting bitmap to cache for key: " + key);
      mMemoryCache.put(key, bitmap);
    }
  }

  public Bitmap getBitmapFromMemCache(String key)
  {
    Log.e("Bitmap Helper", "Loading bitmap from cache for key: " + key);
    return mMemoryCache.get(key);
  }
}




Java Source Code List

com.ctc.android.widget.BitmapHelper.java
com.ctc.android.widget.ImageMapTestActivity.java
com.ctc.android.widget.ImageMap.java
com.licubeclub.zionhs.Appinfo.java
com.licubeclub.zionhs.Doc_Contributors.java
com.licubeclub.zionhs.Doc_Copying.java
com.licubeclub.zionhs.Doc_Notices.java
com.licubeclub.zionhs.Doc_Readme.java
com.licubeclub.zionhs.DrawerListAdapter.java
com.licubeclub.zionhs.ListCalendarAdapter.java
com.licubeclub.zionhs.MainActivity.java
com.licubeclub.zionhs.MealActivity3.java
com.licubeclub.zionhs.MealLoadHelper.java
com.licubeclub.zionhs.Notices_Parents.java
com.licubeclub.zionhs.Notices.java
com.licubeclub.zionhs.PostListAdapter.java
com.licubeclub.zionhs.Schedule.java
com.licubeclub.zionhs.Schoolinfo.java
com.licubeclub.zionhs.Schoolintro.java
com.licubeclub.zionhs.WebViewActivity.java
com.licubeclub.zionhs.meal.FridayMeal.java
com.licubeclub.zionhs.meal.MondayMeal.java
com.licubeclub.zionhs.meal.ThursdayMeal.java
com.licubeclub.zionhs.meal.TuesdayMeal.java
com.licubeclub.zionhs.meal.WednsdayMeal.java