Android Open Source - android-gskbyte-utils L R U Bitmap Manager






From Project

Back to project page android-gskbyte-utils.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android-gskbyte-utils 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 org.gskbyte.bitmap;
/*from ww  w .  j  a  v  a2s .c o  m*/
import org.gskbyte.util.Logger;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;

/**
 * LRUBitmapManager class
 * 
 * This implementation of AbstractBitmapManager uses an LRUCache to store bitmaps
 * and refer to them. This allows the programmer not to abuse of memory usage, while not
 * keeping too much attention on it.
 * 
 * */
public class LRUBitmapManager
extends AbstractBitmapManager
{

public static final float DEFAULT_MEMORY_RATE = 0.33f;

private final LRUBitmapCache<String> bitmapCache;

/**
 * Constructor. Uses a memory rate of 25%, which is a good value when this
 * is the only LRUBitmapCache present in an application.
 * @param context The application context, needed to load files.
 * */
public LRUBitmapManager(Context context)
{
    this(context, DEFAULT_MEMORY_RATE);
}

/**
 * Constructor. Allows to specify a maximum memory rate. Low values will cause the Bitmaps to load
 * frequently, which can lead to a slow application. Too high values can provoke an OutOfMemoryError.
 * @param context The application context, needed to load files.
 * @param memoryRate The maximum application's memory rate to be used by this manager.
 * */
public LRUBitmapManager(Context context, float memoryRate)
{
    super(context);
    final int maxSize = MaxMemorySizeForRate(context, memoryRate);
    this.bitmapCache = new LRUBitmapCache<String>(maxSize);
}

public LRUBitmapManager(Context context, int numLoadThreads, float memoryRate)
{
    super(context, numLoadThreads);
    final int maxSize = MaxMemorySizeForRate(context, memoryRate);
    this.bitmapCache = new LRUBitmapCache<String>(maxSize);
}

public static final int MaxMemorySizeForRate(Context context, float memoryRate)
{
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    final int totalMemoryInMegabytes = activityManager.getMemoryClass();
    final int maxMemoryInMegabytes = (int)(totalMemoryInMegabytes * memoryRate);
    final int maxMemoryInBytes = (int) (maxMemoryInMegabytes * 1024 * 1024);
    return maxMemoryInBytes;
}

protected BitmapRef initializeReference(int location, String path)
{
    return new LRUBitmapRef(location, path);
}


@Override
public void clear()
{
    super.clear();
    bitmapCache.evictAll();
}

/**
 * This could be optimized having a counter, but this method is not very likely
 * to be used, we keep it simple.
 * */
@Override
public int countLoadedBitmaps()
{
    int count = 0;
    for(String s : references.keySet()) {
        if(bitmapCache.get(s) != null)
            ++count;
    }
    
    return count;
}

@Override
public void releaseAllBitmaps()
{
    bitmapCache.evictAll();
}

/**
 * Frees memory from old bitmaps, given the rate of memory that we want to free up (0 = nothing, 1 = all bitmaps)
 * @param freeUpRate The amount of memory to free up
 * */
public void releaseOldBitmaps(float freeUpRate)
{
    final int initialSize = bitmapCache.size();
    
    int maxSize = -1; // Removes all, equivalent to releaseAll
    if(freeUpRate < 0)
        return;
    if(freeUpRate < 1) {
        int cacheMaxSize = bitmapCache.getMaxSize();
        maxSize = (int)((1-freeUpRate)*cacheMaxSize);
    }
    
    bitmapCache.trimToSize(maxSize);
    final int finalSize = bitmapCache.size();
    final int sizeDif = initialSize - finalSize;
    if(sizeDif != 0) {
        Logger.info( getClass(), String.format("Freed memory: %.2f MB before, %.2f MB after (%.2f MB saved)",
                (initialSize/(1024*1024f)),
                (finalSize/(1024*1024f)),
                (sizeDif/(1024*1024f))
                ) );
    }
}

/**
 * Specialization of a BitmapReference. Refers to Bitmaps from the Manager's LRUCache.
 * */
final class LRUBitmapRef
extends AbstractBitmapManager.BitmapRef
{

public LRUBitmapRef(int location, String path)
{
    super(location, path);
}

@Override
public boolean isLoaded()
{
    return bitmapCache.get(path) != null;
}

@Override
public Bitmap getBitmap(ScaleMode scaleMode, int maxWidth, int maxHeight)
{
    Bitmap bitmap = bitmapCache.get(path);
    if(bitmap == null) {
        bitmap = loadBitmap(scaleMode, maxWidth, maxHeight);
        if(bitmap != null) {
            bitmapCache.put(path, bitmap);
        } else {
            Logger.error(getClass(), "Bitmap not found: " + path);
        }
    }
    return bitmap;
}

@Override
public void freeResources()
{
    bitmapCache.remove(path);
}

}

}




Java Source Code List

com.woozzu.android.widget.IndexScroller.java
com.woozzu.android.widget.IndexableListView.java
org.gskbyte.FragmentWrapperActivity.java
org.gskbyte.animation.ExpandAnimation.java
org.gskbyte.bitmap.AbstractBitmapManager.java
org.gskbyte.bitmap.BitmapColorizer.java
org.gskbyte.bitmap.BitmapManager.java
org.gskbyte.bitmap.CachedBitmapColorizer.java
org.gskbyte.bitmap.IndexedBitmaps.java
org.gskbyte.bitmap.LRUBitmapCache.java
org.gskbyte.bitmap.LRUBitmapManager.java
org.gskbyte.bitmap.PrivateBitmapManager.java
org.gskbyte.bitmap.ReferencedBitmaps.java
org.gskbyte.collection.ArrayHashMap.java
org.gskbyte.collection.DoubleSparseArray.java
org.gskbyte.collection.ListHashMap.java
org.gskbyte.dialog.DownloadDialogFragment.java
org.gskbyte.dialog.LoadDialogFragment.java
org.gskbyte.dialog.OpenLinkDialogBuilder.java
org.gskbyte.dialog.PickerDialogFragment.java
org.gskbyte.download.DiskDownload.java
org.gskbyte.download.DownloadManager.java
org.gskbyte.download.Download.java
org.gskbyte.download.MemoryDownload.java
org.gskbyte.drawable.AutoBackgroundButtonDrawable.java
org.gskbyte.listener.IListenable.java
org.gskbyte.listener.ListenableNG.java
org.gskbyte.listener.Listenable.java
org.gskbyte.preferences.DialogSeekBarPreference.java
org.gskbyte.preferences.InlineSeekBarPreference.java
org.gskbyte.remote.AsyncURLRequest.java
org.gskbyte.remote.URLRequest.java
org.gskbyte.tasks.QueuedTaskExecutor.java
org.gskbyte.tasks.TaskStep.java
org.gskbyte.tasks.Task.java
org.gskbyte.ui.ArrayAdapterWithDefaultValue.java
org.gskbyte.ui.ListAdapter.java
org.gskbyte.ui.ColorDialog.ColorDialog.java
org.gskbyte.ui.ColorDialog.ColorPreference.java
org.gskbyte.ui.iconifiedMainMenuList.EntryView.java
org.gskbyte.ui.iconifiedMainMenuList.MainMenuAdapter.java
org.gskbyte.ui.iconifiedMainMenuList.MenuEntry.java
org.gskbyte.util.FrequentIntents.java
org.gskbyte.util.IOUtils.java
org.gskbyte.util.Logger.java
org.gskbyte.util.OpenFileHandlerFactory.java
org.gskbyte.util.OpenFileHandler.java
org.gskbyte.util.XmlUtils.java
org.gskbyte.view.AsyncImageView.java
org.gskbyte.view.AutoBackgroundButton.java
org.gskbyte.view.AutoBackgroundImageButton.java
org.gskbyte.view.AutoHeightImageView.java
org.gskbyte.view.ExpandedGridView.java
org.gskbyte.view.ExpandedListView.java
org.gskbyte.view.FontUtil.java
org.gskbyte.view.FontableButton.java
org.gskbyte.view.FontableCheckBox.java
org.gskbyte.view.FontableEditText.java
org.gskbyte.view.FontableTextView.java
org.gskbyte.view.FullWidthImageView.java
org.gskbyte.view.ProportionalHeightLayout.java
org.gskbyte.view.PullToRefreshListView.java
org.gskbyte.view.SquaredLayout.java
org.gskbyte.view.StepSeekBar.java
org.gskbyte.view.TextViewUtil.java
org.gskbyte.view.ViewUtils.java