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.itsherpa.andg.ui.account.ImageCache.java

/**
 * Initialize the cache./*w  w w  . j a  va2s.c o m*/
 *
 * @param memCacheSizePercent The cache size as a percent of available app memory.
 */
private void init(float memCacheSizePercent) {
    int memCacheSize = calculateMemCacheSize(memCacheSizePercent);

    // Set up memory cache
    if (BuildConfig.DEBUG) {
        LogUtils.d(TAG, "Memory cache created (size = " + memCacheSize + ")");
    }
    mMemoryCache = new LruCache<String, Bitmap>(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;
        }
    };
}

From source file:com.ohso.util.ImageHandler.java

public ImageHandler(Activity activity) {
    MEMORY_CLASS = ((ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
    MEMORY_CACHE_SIZE = 1024 * 1024 * MEMORY_CLASS / 8; // Use 1/8 the available memory
    mMemoryCache = new LruCache<String, Bitmap>(MEMORY_CACHE_SIZE) {
        protected int sizeOf(String key, Bitmap bitmap) {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }/*www. java  2  s. c om*/
    };
    File cacheDir = new File(activity.getCacheDir(), "thumbnails");
    try {
        mDiskCache = DiskLruCache.open(cacheDir, 1, 1, DISK_CACHE_SIZE);
    } catch (IOException e) {
        e.printStackTrace();
    }
    mPixelSize = scaledDIP(IMAGE_DIMENSIONS);
    mPlaceholderParams = new RelativeLayout.LayoutParams((int) mPixelSize, (int) mPixelSize);
    mPlaceholderParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    mPlaceholderParams.topMargin = 0;
}

From source file:com.itsherpa.andg.imageloader.ContactImageCache.java

/**
 * Initialize the cache./*from   w w w  .j av  a2s  . c  o m*/
 * 
 * @param memCacheSizePercent
 *            The cache size as a percent of available app memory.
 */
private void init(float memCacheSizePercent) {
    int memCacheSize = calculateMemCacheSize(memCacheSizePercent);

    // Set up memory cache
    if (BuildConfig.DEBUG) {
        LogUtils.d(TAG, "Memory cache created (size = " + memCacheSize + ")");
    }
    mMemoryCache = new LruCache<String, Bitmap>(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;
        }
    };
}

From source file:com.witheyjr.listviewanimator.StableWrapperArrayAdapter.java

public StableWrapperArrayAdapter(Context context, int layoutResourceId, List<ContentsWrapper> origObjects) {
    super(context, layoutResourceId, origObjects);
    this.mObjects = origObjects;
    for (int i = 0; i < mObjects.size(); ++i) {
        mIdMap.put(mObjects.get(i), i);/*from w w w.  j a  v  a2  s.  c o m*/
    }
    this.mContext = context;
    this.mLayoutResourceId = layoutResourceId;
    withImage = true;
    /* Get max available VM memory, exceeding this amount will throw an OutOfMemory exception. 
     * Stored in kilobytes as LruCache takes an int in its constructor.*/
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    // Use 1/4 of the available memory for this memory cache.
    final int cacheSize = maxMemory / 4;
    mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
        @SuppressLint("NewApi")
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes rather than number of items. Also, I love fragmentation.
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
                return (bitmap.getRowBytes() * bitmap.getHeight()) / 1024;
            } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                return bitmap.getByteCount() / 1024;
            } else {
                return bitmap.getAllocationByteCount() / 1024;
            }
        }
    };
}

From source file:cs.man.ac.uk.tavernamobile.utils.TavernaAndroid.java

/**** for testing purpose ****/

@Override//from   ww  w.j av  a2  s.  c  o  m
public void onCreate() {
    // ACRA.init(this);
    super.onCreate();

    // Get max available VM memory,
    // since exceeding this amount will throw an OutOfMemory exception.
    final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

    // Use 1/8th of the available memory for this memory cache.
    final int cacheSize = maxMemory / 8;

    mImageCache = new LruCache<String, Bitmap>(cacheSize) {
        @Override
        protected int sizeOf(String key, Bitmap bitmap) {
            // The cache size will be measured in kilobytes
            return bitmap.getByteCount() / 1024;
        }
    };

    mCache = new HashMap<String, Object>();
}

From source file:github.daneren2005.dsub.service.CachedMusicService.java

public CachedMusicService(MusicService musicService) {
    this.musicService = musicService;
    cachedMusicDirectories = new LruCache<String, TimeLimitedCache<MusicDirectory>>(MUSIC_DIR_CACHE_SIZE);
}

From source file:io.github.clendy.leanback.widget.ViewsStateBundle.java

protected void applyPolicyChanges() {
    if (mSavePolicy == BaseGridView.SAVE_LIMITED_CHILD) {
        if (mLimitNumber <= 0) {
            throw new IllegalArgumentException();
        }// w  ww  .  j  a v a  2  s. co  m
        if (mChildStates == null || mChildStates.maxSize() != mLimitNumber) {
            mChildStates = new LruCache<String, SparseArray<Parcelable>>(mLimitNumber);
        }
    } else if (mSavePolicy == BaseGridView.SAVE_ALL_CHILD || mSavePolicy == BaseGridView.SAVE_ON_SCREEN_CHILD) {
        if (mChildStates == null || mChildStates.maxSize() != UNLIMITED) {
            mChildStates = new LruCache<String, SparseArray<Parcelable>>(UNLIMITED);
        }
    } else {
        mChildStates = null;
    }
}

From source file:org.opensilk.music.cast.CastWebServer.java

public CastWebServer(Context context, String host, int port) {
    super(host, port);
    mContext = context;/*w  w  w .  ja  v  a2 s .  co  m*/
    // get the lock
    mWifiLock = ((WifiManager) mContext.getSystemService(Context.WIFI_SERVICE))
            .createWifiLock(WifiManager.WIFI_MODE_FULL, "CastServer");
    mWifiLock.setReferenceCounted(false);
    // arbitrary size might increase as needed;
    mEtagCache = new LruCache<>(20);
    mBytePool = new ByteArrayPool(2 * 1024 * 1024);
}

From source file:net.gree.asdk.core.imageloader.cache.ImageCache.java

private void initMemCache(Context context) {
    mMemCache = new LruCache<String, Bitmap>(mSettings.mMemCacheSize) {
        @Override//from ww w  .java 2  s.c  o m
        protected int sizeOf(String key, Bitmap bitmap) {
            return getBitmapSize(bitmap);
        }

        @Override
        protected void entryRemoved(boolean evicted, String key, Bitmap oldBitmap, Bitmap newBitmap) {
            if (oldBitmap != null && !oldBitmap.isRecycled()) {
                oldBitmap.recycle();
                oldBitmap = null;
            }
        }
    };
}

From source file:com.mysentosa.android.sg.imageloader.ImageCache.java

/**
 * Initialize the cache, providing all parameters.
 *
 * @param context The context to use//from www.j  ava  2  s  .c  o m
 * @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 ImageLoaderUtils.getBitmapSize(bitmap);
            }
        };
    }
    SharedPreferences mPrefs;
    mPrefs = context.getSharedPreferences(CACHE_PREFS, Context.MODE_PRIVATE);
    int CACHE_VERSION_NUMBER = mPrefs.getInt(ImageCacheParams.CACHE_VERSIOON_PREF, -1);
    if (CACHE_VERSION_NUMBER < ImageCacheParams.CACHE_VERSION) {
        clearCaches();
        // commit database version to shared prefs
        final SharedPreferences.Editor edit = mPrefs.edit();
        edit.clear();
        edit.putInt(ImageCacheParams.CACHE_VERSIOON_PREF, ImageCacheParams.CACHE_VERSION);
        edit.commit();
    }
}