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






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   w  w w.j  a va 2  s  . c om
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v4.util.LruCache;

/**
 * An LRU Cache for bitmaps.
 * Contains helper methods to compute bitmap memory size, because they are active
 * only in newer Android versions
 * */
public class LRUBitmapCache<KeyClass>
extends LruCache<KeyClass, Bitmap>
{

private final int maxSize;

/**
 * Constructs an LRU cache with the given max size
 * @param maxSize The maximum cache size, in bytes
 * */
public LRUBitmapCache(int maxSize)
{ super(maxSize); this.maxSize = maxSize; }

/**
 * Returns the maximum size of the whole cache, in bytes.
 * @return the maximum cache size in bytes
 * */
public int getMaxSize()
{ return maxSize; }

/**
 * Returns the size of a bitmap given its key. Check the method sizeOf in Android's LRUCache class documentation
 * @param key the key of the saved bitmap
 * @param value the bitmap for which to get the size
 * */
@Override
protected int sizeOf(KeyClass key, Bitmap value)
{ return BitmapMemorySize(value); }

/**
 * Returns the size of a Bitmap given its key
 * @param key The key of the Bitmap to retrieve
 * @return the size in bytes, or -1 if the bitmap is not managed
 * */
public int sizeOf(KeyClass key)
{
    Bitmap b = get(key);
    if(b != null) {
        return BitmapMemorySize( get(key) );
    } else {
        return -1;
    }
}

/**
 * Returns number of bytes per pixel given a bitmap config.
 * */
public final static int BytesPerPixel(Bitmap.Config config)
{
    switch(config) {
    case ALPHA_8:
        return 1;
    case ARGB_8888:
        return 4;
    
    default:
    case ARGB_4444:
    case RGB_565:
        return 2;
    }
}

/**
 * Returns the memory required for a bitmap.
 * @param bitmap The bitmap for which to calculate its size.
 * @return Its size, in bytes.
 * */
@SuppressLint("NewApi")
public static int BitmapMemorySize(Bitmap bitmap)
{
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {
        return bitmap.getByteCount();
    } else {
        return bitmap.getWidth() * bitmap.getHeight() * BytesPerPixel(bitmap.getConfig());
    }
}

/**
 * Returns the memory required for a bitmap configuration
 * @param width The bitmap's width.
 * @param height The bitmap's height.
 * @param config The bitmap's color configuration.
 * @return Its size, in bytes.
 * */
public final static int BitmapMemorySize(int width, int height, Bitmap.Config config)
{ return width * height * BytesPerPixel(config); }


}




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