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:rhcad.touchvg.view.internal.ImageCache.java

private boolean createCache() {
    try {//  w  w  w. j a  va 2  s  .com
        mCache = new LruCache<String, Drawable>(CACHE_SIZE) {
            @Override
            protected int sizeOf(String key, Drawable d) {
                // TODO: SVG size?
                int size = 1;
                if (d.getClass().isInstance(BitmapDrawable.class)) {
                    size = ((BitmapDrawable) d).getBitmap().getByteCount();
                }
                return size;
            }
        };
    } catch (NoClassDefFoundError e) {
        Log.e(TAG, "Need android-support-v4.jar in application", e);
    }
    return mCache != null;
}

From source file:com.apptentive.android.sdk.util.cache.ImageMemoryCache.java

public ImageMemoryCache(int maxMega) {
    bufferCache = new LruCache<String, Object>(maxMega * 1024 * 1024) { // by default use 1M as a unit for the in memory Lrucache
        /**//www .j a v  a2  s.  c om
         * recycle the removed bitmap from memory
         */
        @Override
        protected void entryRemoved(boolean evicted, String key, Object oldValue, Object newValue) {
            if (oldValue != null) {
                if (oldValue instanceof Bitmap) {
                    ((Bitmap) oldValue).recycle();
                } else if (oldValue instanceof BitmapDrawable) {
                    ((BitmapDrawable) oldValue).getBitmap().recycle();
                }
                oldValue = null;
            }
        }

        @Override
        protected int sizeOf(String key, Object object) {
            // The cache size will be measured in bytes rather than
            // number of items.
            int byteCount = 0;

            if (object instanceof Bitmap) {
                byteCount = ((Bitmap) object).getRowBytes() * ((Bitmap) object).getHeight();
            } else if (object instanceof BitmapDrawable) {
                Bitmap bm = ((BitmapDrawable) object).getBitmap();
                byteCount = bm.getRowBytes() * bm.getHeight();
            }
            return byteCount;
        }
    };
}

From source file:com.dp.chapter01.part2.cache.MemoryCache.java

public MemoryCache() {
    // ?//from w w  w . jav  a 2s  . co m
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    // ?4?
    final int cacheSize = maxMemory / 4;
    mMemeryCache = new LruCache<String, Bitmap>(cacheSize) {

        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
        }
    };

}

From source file:com.amingnet.LruMemCache.java

public LruMemCache() {
    // ?//w w w . j  a va  2s .c  o  m
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // ??
    final int cacheSize = maxMemory / 8;
    mResponseCache = new LruCache<String, AResponse>(cacheSize) {

        @Override
        protected int sizeOf(String key, AResponse response) {
            return response.rawData.length / 1024;
        }
    };

}

From source file:app.philm.in.util.TypefaceManager.java

public TypefaceManager(AssetManager assetManager) {
    mAssetManager = Preconditions.checkNotNull(assetManager, "assetManager cannot be null");
    mCache = new LruCache<>(3);
}

From source file:poisondog.android.image.ImageCache.java

public ImageCache(Context context, FileObject cacheDirectory) {
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    final int cacheSize = maxMemory / 4;

    mMemoryCache = new LruCache<String, BitmapDrawable>(cacheSize) {
        @Override/*from  ww  w  . j  a  v  a  2 s  .c  o  m*/
        protected void entryRemoved(boolean evicted, String key, BitmapDrawable oldValue,
                BitmapDrawable newValue) {
            if (RecyclingBitmapDrawable.class.isInstance(oldValue)) {
                ((RecyclingBitmapDrawable) oldValue).setIsCached(false);
            }
        }

        @Override
        protected int sizeOf(String key, BitmapDrawable bitmap) {
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            bitmap.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, bao);
            return bao.toByteArray().length / 1024;
        }
    };
    new InitDiskCacheTask().execute(cacheDirectory);
}

From source file:com.sckftr.android.utils.TypefaceManager.java

public TypefaceManager(AssetManager assetManager) {
    if (assetManager == null)
        throw new IllegalArgumentException("assetManager cannot be null");
    mAssetManager = assetManager;/*from w  w w  . jav  a2  s. c o  m*/
    mCache = new LruCache<String, Typeface>(3);
}

From source file:cn.shinsoft.Cache.java

public static synchronized void initialize(Configuration configuration) {
    if (sIsInitialized) {
        Log.v("ActiveAndroid already initialized.");
        return;//from w w w.  ja  v  a  2s.  c  o  m
    }

    sContext = configuration.getContext();
    sModelInfo = new ModelInfo(configuration);
    sDatabaseHelper = new DatabaseHelper(configuration);

    // TODO: It would be nice to override sizeOf here and calculate the memory
    // actually used, however at this point it seems like the reflection
    // required would be too costly to be of any benefit. We'll just set a max
    // object size instead.
    sEntities = new LruCache<String, Model>(configuration.getCacheSize());

    openDatabase();

    sIsInitialized = true;

    Log.v("ActiveAndroid initialized successfully.");
}

From source file:com.chromium.fontinstaller.ui.fontlist.FontListAdapter.java

public FontListAdapter(Context context, ArrayList<String> fontNames, boolean enableTrueFont) {
    this.fontNames = fontNames;
    this.enableTrueFont = enableTrueFont;

    setHasStableIds(true);//from w ww .j  a v a2  s. c om

    fontCache = new LruCache<>(FileUtils.getMaxCacheSize(context));
}

From source file:com.appsimobile.appsii.plugins.IconCache.java

@Inject
public IconCache(ActivityManager activityManager) {
    final int memClass = activityManager.getMemoryClass();
    int sizeInBytes = memClass * 1024 * 1024;
    int desiredSize = sizeInBytes / 20;
    // for 50 mb devices this will be 2.5 mb
    // for 12 mb devices this is 0.6 mb
    mCacheSize = desiredSize;/* www. jav a 2 s  .  co  m*/
    mCache = new LruCache<CacheKey, Bitmap>(desiredSize) {
        @Override
        protected int sizeOf(CacheKey key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    };
}