Example usage for android.support.v4.util LruCache LruCache

List of usage examples for android.support.v4.util LruCache LruCache

Introduction

In this page you can find the example usage for android.support.v4.util LruCache LruCache.

Prototype

public LruCache(int maxSize) 

Source Link

Usage

From source file:com.rsegismont.androlife.common.utils.ImageCacher.java

/**
 * Initialize the cache, providing all parameters.
 * /*from  w  w w . java2  s .  co  m*/
 * @param cacheParams
 *            The cache parameters to initialize the cache
 */
@SuppressLint("UseSparseArrays")
private void init(ImageCacheParams cacheParams) {
    mCacheParams = cacheParams;

    // Set up memory cache
    if (mCacheParams.memoryCacheEnabled) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
        }

        // If we're running on Honeycomb or newer, then
        if (Utils.hasHoneycomb()) {
            mReusableBitmaps = new ConcurrentSkipListMap<Integer, SoftReference<Bitmap>>();
        }

        mMemoryCacheSize = new HashMap<String, Point>();

        mMemoryCacheData = new LruCache<String, BitmapDrawable>(mCacheParams.memCacheSize) {

            /**
             * Notify the removed entry that is no longer being cached
             */
            @Override
            protected void entryRemoved(boolean evicted, String key, BitmapDrawable oldValue,
                    BitmapDrawable newValue) {
                if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
                    // The removed entry is a recycling drawable, so notify
                    // it
                    // that it has been removed from the memory cache
                    ((RecyclingBitmapDrawable) oldValue).setIsCached(false);
                } else {
                    // The removed entry is a standard BitmapDrawable

                    if (Utils.hasHoneycomb()) {
                        // We're running on Honeycomb or later, so add the
                        // bitmap
                        // to a SoftRefrence set for possible use with
                        // inBitmap later

                        synchronized (mMemCacheLock) {
                            if (evicted == true) {
                                mReusableBitmaps.put(getBitmapSize(oldValue),
                                        new SoftReference<Bitmap>(oldValue.getBitmap()));
                            }
                        }
                    }
                }
            }

            /**
             * Measure item size in kilobytes rather than units which is
             * more practical for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, BitmapDrawable value) {
                final int bitmapSize = getBitmapSize(value) / 1024;
                return bitmapSize == 0 ? 1 : bitmapSize;
            }
        };
    }

    // By default the disk cache is not initialized here as it should be
    // initialized
    // on a separate thread due to disk access.
    if (cacheParams.initDiskCacheOnCreate) {
        // Set up disk cache
        initDiskCache();
    }
}

From source file:com.tack.android.image.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 *
 * @param cacheParams The cache parameters to initialize the cache
 *//*from   w  w  w  .jav  a 2  s.c o m*/
private void init(ImageCacheParams cacheParams) {
    mCacheParams = cacheParams;

    // Set up memory cache
    if (mCacheParams.memoryCacheEnabled) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
        }
        mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
            /**
             * Measure item size in kilobytes rather than units which is more practical
             * for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                final int bitmapSize = BitmapHelper.getBitmapSize(bitmap) / 1024;
                return bitmapSize == 0 ? 1 : bitmapSize;
            }
        };
    }

    // By default the disk cache is not initialized here as it should be initialized
    // on a separate thread due to disk access.
    if (cacheParams.initDiskCacheOnCreate) {
        // Set up disk cache
        initDiskCache();
    }
}

From source file:uk.org.ngo.squeezer.util.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 *
 * @param cacheParams The cache parameters to initialize the cache
 *///from   w w w.j ava  2  s .  co  m
private void init(ImageCacheParams cacheParams) {
    mCacheParams = cacheParams;

    // Set up memory cache
    if (mCacheParams.memoryCacheEnabled) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
        }
        mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return (bitmap.getRowBytes() * bitmap.getHeight());
            }
        };
    }

    // By default the disk cache is not initialized here as it should be initialized
    // on a separate thread due to disk access.
    if (cacheParams.initDiskCacheOnCreate) {
        // Set up disk cache
        initDiskCache();
    }
}

From source file:com.andreaszeiser.bob.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 * // ww  w.  j  ava 2 s . c o m
 * @param cacheParams
 *            The cache parameters to initialize the cache
 */
private void init(ImageCacheParams cacheParams) {
    mCacheParams = cacheParams;

    // Set up memory cache
    if (mCacheParams.memoryCacheEnabled) {
        Log.v(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
        mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
            /**
             * Measure item size in kilobytes rather than units which is
             * more practical for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                final int bitmapSize = getBitmapSize(bitmap) / 1024;
                return bitmapSize == 0 ? 1 : bitmapSize;
            }
        };
    }

    // By default the disk cache is not initialized here as it should be
    // initialized
    // on a separate thread due to disk access.
    if (cacheParams.initDiskCacheOnCreate) {
        // Set up disk cache
        initDiskCache();
    }
}

From source file:com.android.project.imagefetcher.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 * /*  ww  w.  j  ava 2  s .  c om*/
 * @param cacheParams
 *            The cache parameters to initialize the cache
 */
private void init(ImageCacheParams cacheParams) {
    mCacheParams = cacheParams;

    // Set up memory cache
    if (mCacheParams.memoryCacheEnabled) {
        if (BuildConfig.DEBUG) {
            // Log.d(TAG, "Memory cache created (size = "
            // + mCacheParams.memCacheSize + ")");
        }
        mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
            /**
             * Measure item size in bytes rather than units which is more
             * practical for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return getBitmapSize(bitmap);
            }
        };
    }

    // By default the disk cache is not initialized here as it should be
    // initialized
    // on a separate thread due to disk access.
    if (cacheParams.initDiskCacheOnCreate) {
        // Set up disk cache
        initDiskCache();
    }
}

From source file:com.example.demo.util.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 *
 * @param context The context to use//from ww  w  .ja v  a2s. c om
 * @param cacheParams The cache parameters to initialize the cache
 */
private void init(Context context, ImageCacheParams cacheParams) {
    /*        final File diskCacheDir = DiskLruCache.getDiskCacheDir(context, cacheParams.uniqueName);
            
            // Set up disk cache
            if (cacheParams.diskCacheEnabled) {
    mDiskCache = DiskLruCache.openCache(context, diskCacheDir, cacheParams.diskCacheSize);
    mDiskCache.setCompressParams(cacheParams.compressFormat, cacheParams.compressQuality);
    if (cacheParams.clearDiskCacheOnStart) {
        mDiskCache.clearCache();
    }
            }
    */
    // Set up memory cache
    if (cacheParams.memoryCacheEnabled) {
        mMemoryCache = new LruCache<String, Bitmap>(cacheParams.memCacheSize) {
            /**
             * Measure item size in bytes rather than units which is more practical for a bitmap
             * cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return getBitmapSize(bitmap);
            }
        };
    }
}

From source file:com.projectsexception.myapplist.iconloader.IconManager.java

/**
 * Constructs the work queues and thread pools used to download and decode images.
 *//*from ww w .java  2 s  . co  m*/
private IconManager() {

    /*
     * Creates a work queue for the pool of Thread objects used for downloading, using a linked
     * list queue that blocks when the queue is empty.
     */
    mDownloadWorkQueue = new LinkedBlockingQueue<Runnable>();

    /*
     * Creates a work queue for the set of of task objects that control downloading and
     * decoding, using a linked list queue that blocks when the queue is empty.
     */
    mIconTaskWorkQueue = new LinkedBlockingQueue<IconTask>();

    /*
     * Creates a new pool of Thread objects for the download work queue
     */
    mDownloadThreadPool = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_TIME,
            KEEP_ALIVE_TIME_UNIT, mDownloadWorkQueue);

    // Instantiates a new cache based on the cache size estimate
    mIconCache = new LruCache<String, Drawable>(IMAGE_CACHE_SIZE);

    /*
     * Instantiates a new anonymous Handler object and defines its
     * handleMessage() method. The Handler *must* run on the UI thread, because it moves
     * Drawables from the IconTask object to the View object.
     * To force the Handler to run on the UI thread, it's defined as part of the IconManager
     * constructor. The constructor is invoked when the class is first referenced, and that
     * happens when the View invokes startDownload. Since the View runs on the UI Thread, so
     * does the constructor and the Handler.
     */
    mHandler = new Handler(Looper.getMainLooper()) {

        /*
         * handleMessage() defines the operations to perform when the
         * Handler receives a new Message to process.
         */
        @Override
        public void handleMessage(Message inputMessage) {

            // Gets the image task from the incoming Message object.
            IconTask iconTask = (IconTask) inputMessage.obj;

            // Sets an IconView that's a weak reference to the
            // input ImageView
            IconView localView = iconTask.getIconView();

            // If this input view isn't null
            if (localView != null) {

                /*
                 * Gets the package name of the *weak reference* to the input
                 * ImageView. The weak reference won't have changed, even if
                 * the input ImageView has.
                 */
                String packageName = localView.getPackageName();

                /*
                 * Compares the URL of the input ImageView to the URL of the
                 * weak reference. Only updates the drawable in the ImageView
                 * if this particular Thread is supposed to be serving the
                 * ImageView.
                 */
                if (iconTask.getPackageName() != null && iconTask.getPackageName().equals(packageName)) {

                    /*
                     * Chooses the action to take, based on the incoming message
                     */
                    switch (inputMessage.what) {

                    // If the download has started, sets background color to dark green
                    case LOAD_STARTED:
                        localView.setStatusResource(R.drawable.ic_default_launcher);
                        break;
                    /*
                     * The decoding is done, so this sets the
                     * ImageView's bitmap to the bitmap in the
                     * incoming message
                     */
                    case TASK_COMPLETE:
                        localView.setImageDrawable(iconTask.getDrawable());
                        recycleTask(iconTask);
                        break;
                    // The download failed, sets the background color to dark red
                    case LOAD_FAILED:
                        localView.setStatusResource(R.drawable.ic_default_launcher);

                        // Attempts to re-use the Task object
                        recycleTask(iconTask);
                        break;
                    default:
                        // Otherwise, calls the super method
                        super.handleMessage(inputMessage);
                    }
                }
            }
        }
    };
}

From source file:com.emuneee.superb.ui.bitmap.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 * /* w ww  .  j a  v  a  2s .c  o m*/
 * @param context
 *            The context to use
 * @param cacheParams
 *            The cache parameters to initialize the cache
 */
private void init(Context context, ImageCacheParams cacheParams) {
    final File diskCacheDir = DiskLruCache.getDiskCacheDir(context, cacheParams.uniqueName);

    // Set up disk cache
    if (cacheParams.diskCacheEnabled) {
        mDiskCache = DiskLruCache.openCache(context, diskCacheDir, cacheParams.diskCacheSize);
        mDiskCache.setCompressParams(cacheParams.compressFormat, cacheParams.compressQuality);
        if (cacheParams.clearDiskCacheOnStart) {
            mDiskCache.clearCache();
        }
    }

    // Set up memory cache
    if (cacheParams.memoryCacheEnabled) {
        mMemoryCache = new LruCache<String, Bitmap>(cacheParams.memCacheSize) {
            /**
             * Measure item size in bytes rather than units which is more
             * practical for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return Utils.getBitmapSize(bitmap);
            }
        };
    }
}

From source file:com.fireplace.market.fads.util.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 * // w ww .ja v  a 2 s  .  c om
 * @param cacheParams
 *            The cache parameters to initialize the cache
 */
private void init(ImageCacheParams cacheParams) {
    mCacheParams = cacheParams;

    // Set up memory cache
    if (mCacheParams.memoryCacheEnabled) {
        if (FireplaceApplication.DEBUG) {
            Log.d(TAG, "Memory cache created (size = " + mCacheParams.memCacheSize + ")");
        }
        mMemoryCache = new LruCache<String, Bitmap>(mCacheParams.memCacheSize) {
            /**
             * Measure item size in bytes rather than units which is more
             * practical for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return getBitmapSize(bitmap);
            }
        };
    }

    // By default the disk cache is not initialized here as it should be
    // initialized
    // on a separate thread due to disk access.
    if (cacheParams.initDiskCacheOnCreate) {
        // Set up disk cache
        initDiskCache();
    }
}

From source file:com.softgame.reddit.cache.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 * //from w  w w.  java2 s  . co  m
 * @param context
 *            The context to use
 * @param cacheParams
 *            The cache parameters to initialize the cache
 */
private void init(Context context, ImageCacheParams cacheParams) {
    final File diskCacheDir = DiskLruCache.getDiskCacheDir(context, cacheParams.uniqueName);

    // Set up disk cache
    if (cacheParams.diskCacheEnabled) {
        mDiskCache = DiskLruCache.openCache(context, diskCacheDir, cacheParams.diskCacheSize);
        // TODO : Reddit Error, Null Point Exception on Init
        if (mDiskCache != null) {
            mDiskCache.setCompressParams(cacheParams.compressFormat, cacheParams.compressQuality);
            if (cacheParams.clearDiskCacheOnStart) {
                mDiskCache.clearCache();
            }
        }
    }

    // Set up memory cache
    if (cacheParams.memoryCacheEnabled) {
        mMemoryCache = new LruCache<String, Bitmap>(cacheParams.memCacheSize) {
            /**
             * Measure item size in bytes rather than units which is more
             * practical for a bitmap cache
             */
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return Utils.getBitmapSize(bitmap);
            }
        };
    }
}