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.androidpi.bricks.gallery.lru.cache.ImageCache.java

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

    // BEGIN_INCLUDE(init_memory_cache)
    // 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, create a set of reusable
        // bitmaps that can be
        // populated into the inBitmap field of BitmapFactory.Options. Note
        // that the set is
        // of SoftReferences which will actually not be very effective due
        // to the garbage
        // collector being aggressive clearing Soft/WeakReferences. A better
        // approach
        // would be to use a strongly references bitmaps, however this would
        // require some
        // balancing of memory usage between this set and the bitmap
        // LruCache. It would also
        // require knowledge of the expected size of the bitmaps. From
        // Honeycomb to JellyBean
        // the size would need to be precise, from KitKat onward the size
        // would just need to
        // be the upper bound (due to changes in how inBitmap can re-use
        // bitmaps).
        if (AppUtil.hasHoneycomb()) {
            mReusableBitmaps = Collections.synchronizedSet(new HashSet<SoftReference<Bitmap>>());
        }

        mMemoryCache = 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 (AppUtil.hasHoneycomb()) {
                        // We're running on Honeycomb or later, so add the
                        // bitmap
                        // to a SoftReference set for possible use with
                        // inBitmap later
                        mReusableBitmaps.add(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;
            }
        };
    }
    // END_INCLUDE(init_memory_cache)

    // 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.emobc.android.ApplicationData.java

public LruCache<String, Drawable> getCache() {
    if (this.cache == null)
        this.cache = new LruCache<String, Drawable>(MAX_CACHE_SIZE);
    return cache;
}

From source file:com.silentcircle.contacts.ContactPhotoManager.java

public ContactPhotoManagerImpl(Context context) {
    mContext = context;/*from   www.  j  av a 2 s.c o  m*/

    final float cacheSizeAdjustment = (MemoryUtils.getTotalMemorySize() >= LARGE_RAM_THRESHOLD) ? 1.0f : 0.5f;
    final int bitmapCacheSize = (int) (cacheSizeAdjustment * BITMAP_CACHE_SIZE);
    mBitmapCache = new LruCache<Object, Bitmap>(bitmapCacheSize) {
        @Override
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
        protected int sizeOf(Object key, Bitmap data) {
            return (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1)
                    ? (data.getRowBytes() * data.getHeight())
                    : data.getByteCount();
        }

        @Override
        protected void entryRemoved(boolean evicted, Object key, Bitmap oldValue, Bitmap newValue) {
            if (DEBUG)
                dumpStats();
        }
    };
    final int holderCacheSize = (int) (cacheSizeAdjustment * HOLDER_CACHE_SIZE);
    mBitmapHolderCache = new LruCache<Object, BitmapHolder>(holderCacheSize) {
        @Override
        protected int sizeOf(Object key, BitmapHolder value) {
            return value.bytes != null ? value.bytes.length : 0;
        }

        @Override
        protected void entryRemoved(boolean evicted, Object key, BitmapHolder oldValue, BitmapHolder newValue) {
            if (DEBUG)
                dumpStats();
        }
    };
    mBitmapHolderCacheRedZoneBytes = (int) (holderCacheSize * 0.75);
    Log.i(TAG, "Cache adj: " + cacheSizeAdjustment);
    if (DEBUG) {
        Log.d(TAG, "Cache size: " + btk(mBitmapHolderCache.maxSize()) + " + " + btk(mBitmapCache.maxSize()));
    }
}

From source file:de.mrapp.android.util.multithreading.AbstractDataBinder.java

/**
 * Creates a new data binder, which uses a specific executor service. Caching is enabled by
 * default. The cache, which is used to store already loaded data, caches up to
 * <code>CACHE_SIZE</code> items.
 *
 * @param context//from www.j a  v a 2s . c  o  m
 *         The context, which should be used by the data binder, as an instance of the class
 *         {@link Context}. The context may not be null
 * @param threadPool
 *         The executor service, which should be used to manage asynchronous tasks, as an
 *         instance of the type {@link ExecutorService}. The executor service may not be null
 */
public AbstractDataBinder(@NonNull final Context context, @NonNull final ExecutorService threadPool) {
    this(context, threadPool, new LruCache<KeyType, DataType>(CACHE_SIZE));
}

From source file:com.DGSD.Teexter.UI.Recipient.BaseRecipientAdapter.java

public BaseRecipientAdapter(Context context, int preferredMaxResultCount) {
    mContext = context;//w  w  w.j a  va  2  s .  c  om
    mContentResolver = context.getContentResolver();
    mInflater = LayoutInflater.from(context);
    mPreferredMaxResultCount = preferredMaxResultCount;
    mPhotoCacheMap = new LruCache<Uri, byte[]>(PHOTO_CACHE_SIZE);
}

From source file:com.serenegiant.testmediastore.MediaStorePhotoAdapter.java

/**
 * create thumbnail cache//from   w w w .j a v  a2  s . c  o  m
 */
@SuppressLint("NewApi")
private final void createBitmapCache(boolean clear) {
    if (clear && (sThumbnailCache != null)) {
        sThumbnailCache.evictAll();
        System.gc();
    }
    if (sThumbnailCache == null) {
        // use 1/CACHE_RATE of available memory as memory cache
        final int cacheSize = 1024 * 1024 * mMemClass / CACHE_RATE; // [MB] => [bytes]
        sThumbnailCache = new LruCache<Long, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(Long key, Bitmap bitmap) {
                // control memory usage instead of bitmap counts
                return bitmap.getRowBytes() * bitmap.getHeight(); // [bytes]
            }
        };
    }
    if (Build.VERSION.SDK_INT >= 9) {
        EXECUTER.allowCoreThreadTimeOut(true); // this makes core threads can terminate  
    }
    // in many case, calling createBitmapCache method means start the new query
    // and need to prepare to run asynchronus tasks
    EXECUTER.prestartAllCoreThreads();
}

From source file:cm.confide.ex.chips.BaseRecipientAdapter.java

public BaseRecipientAdapter(Context context, int preferredMaxResultCount, int queryMode) {
    mContext = context;//from w  w w.jav a  2 s .c  o  m
    mContentResolver = context.getContentResolver();
    mInflater = LayoutInflater.from(context);
    mPreferredMaxResultCount = preferredMaxResultCount;
    mPhotoCacheMap = new LruCache<Uri, byte[]>(PHOTO_CACHE_SIZE);
    mQueryType = queryMode;

    if (queryMode == QUERY_TYPE_EMAIL) {
        mQuery = Queries.EMAIL;
    } else if (queryMode == QUERY_TYPE_PHONE) {
        mQuery = Queries.PHONE;
    } else {
        mQuery = Queries.EMAIL;
        Log.e(TAG, "Unsupported query type: " + queryMode);
    }
}

From source file:com.github.shareme.gwschips.library.BaseRecipientAdapter.java

public BaseRecipientAdapter(Context context, int preferredMaxResultCount, int queryMode) {
    mContext = context;//from   w  w  w . ja va  2s . c o  m
    mContentResolver = context.getContentResolver();
    LayoutInflater mInflater = LayoutInflater.from(context);
    mPreferredMaxResultCount = preferredMaxResultCount;
    if (mPhotoCacheMap == null) {
        mPhotoCacheMap = new LruCache<>(PHOTO_CACHE_SIZE);
    }
    mQueryType = queryMode;

    if (queryMode == QUERY_TYPE_EMAIL) {
        mQuery = Queries.EMAIL;
    } else if (queryMode == QUERY_TYPE_PHONE) {
        mQuery = Queries.PHONE;
    } else {
        mQuery = Queries.EMAIL;
        Log.e(TAG, "Unsupported query type: " + queryMode);
    }
}

From source file:com.android.mtkex.chips.BaseRecipientAdapter.java

public BaseRecipientAdapter(Context context, int preferredMaxResultCount, int queryMode) {
    Log.d(TAG, "[BaseRecipientAdapter] preferredMaxResultCount: " + preferredMaxResultCount + ", queryMode: "
            + queryMode); /// M: MTK debug log
    mContext = context;/*w  ww  .  ja  v a  2 s  .co  m*/
    mContentResolver = context.getContentResolver();
    mInflater = LayoutInflater.from(context);
    mPreferredMaxResultCount = preferredMaxResultCount;
    mPhotoCacheMap = new LruCache<Uri, byte[]>(PHOTO_CACHE_SIZE);
    mQueryType = queryMode;

    if (queryMode == QUERY_TYPE_EMAIL) {
        mQuery = Queries.EMAIL;
    } else if (queryMode == QUERY_TYPE_PHONE) {
        mQuery = Queries.PHONE;
    } else {
        mQuery = Queries.EMAIL;
        Log.e(TAG, "Unsupported query type: " + queryMode);
    }
}

From source file:com.zhongsou.souyue.activity.SplashActivity.java

/**
 * ??//from  www  .j a va 2s. co  m
 */
private void allocMaxMemory() {
    if (mMemoryCache == null) {
        int maxMemory = (int) Runtime.getRuntime().maxMemory() / 1024;
        int cacheSize = maxMemory / 8;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @SuppressLint("NewApi")
            protected int sizeOf(String key, Bitmap value) {
                return value.getByteCount() / 1024;
            }
        };
    }
}