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

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

Introduction

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

Prototype

public final V put(K key, V value) 

Source Link

Document

Caches value for key .

Usage

From source file:com.bobomee.android.common.util.ScreenUtil.java

/**
 * https://gist.github.com/PrashamTrivedi/809d2541776c8c141d9a
 *///w w w .  ja v a 2s.co m
public static Bitmap shotRecyclerView(RecyclerView view) {
    RecyclerView.Adapter adapter = view.getAdapter();
    Bitmap bigBitmap = null;
    if (adapter != null) {
        int size = adapter.getItemCount();
        int height = 0;
        Paint paint = new Paint();
        int iHeight = 0;
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = maxMemory / 8;
        LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);
        for (int i = 0; i < size; i++) {
            RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));
            adapter.onBindViewHolder(holder, i);
            holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),
                    holder.itemView.getMeasuredHeight());
            holder.itemView.setDrawingCacheEnabled(true);
            holder.itemView.buildDrawingCache();
            Bitmap drawingCache = holder.itemView.getDrawingCache();
            if (drawingCache != null) {

                bitmaCache.put(String.valueOf(i), drawingCache);
            }
            height += holder.itemView.getMeasuredHeight();
        }

        bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
        Canvas bigCanvas = new Canvas(bigBitmap);
        Drawable lBackground = view.getBackground();
        if (lBackground instanceof ColorDrawable) {
            ColorDrawable lColorDrawable = (ColorDrawable) lBackground;
            int lColor = lColorDrawable.getColor();
            bigCanvas.drawColor(lColor);
        }

        for (int i = 0; i < size; i++) {
            Bitmap bitmap = bitmaCache.get(String.valueOf(i));
            bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
            iHeight += bitmap.getHeight();
            bitmap.recycle();
        }
    }
    return bigBitmap;
}

From source file:com.robopupu.api.component.BitmapManagerImpl.java

private void addBitmap(final String key, final String cacheName, final Bitmap bitmap,
        final boolean useFileCaching) {
    LruCache<String, Bitmap> cache = bitmapCaches.get(cacheName);

    if (cache == null) {
        cache = addCache(cacheName);/*  ww w .  j ava  2  s. com*/
    }

    cache.put(key, bitmap);

    if (useFileCaching) {
        final Context context = D.get(Context.class);
        final String applicationDirectory = AppToolkit.getApplicationDirectoryPath(context);
        final StringBuilder path = new StringBuilder();
        path.append(applicationDirectory);
        path.append("/");
        path.append(StringToolkit.encodeFileName(cacheName));

        final File cacheDirectory = new File(path.toString());

        if (!cacheDirectory.exists()) {
            if (!cacheDirectory.mkdirs()) {
                throw new IllegalStateException("Failed to create the cache directory");
            }
        }
        path.append("/");
        path.append(StringToolkit.encodeFileName(key));

        try {
            final FileOutputStream outputStream = new FileOutputStream(path.toString());
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
        } catch (Exception e) {
            Log.d(TAG, "Failed to cache a pitmap: " + path);
        }
    }
}

From source file:org.fuusio.api.graphics.BitmapManagerImpl.java

private void addBitmap(final String key, final String cacheName, final Bitmap bitmap,
        final boolean useFileCaching) {
    LruCache<String, Bitmap> cache = mBitmacaches.get(cacheName);

    if (cache == null) {
        cache = addCache(cacheName);/* w ww  .ja v a  2s . com*/
    }

    cache.put(key, bitmap);

    if (useFileCaching) {
        final File applicationDirectory = AppToolkit.getApplicationDirectory();
        final StringBuilder path = new StringBuilder(applicationDirectory.getAbsolutePath());
        path.append("/");
        path.append(StringToolkit.encodeFileName(cacheName));

        final File cacheDirectory = new File(path.toString());

        if (!cacheDirectory.exists()) {
            cacheDirectory.mkdirs();
        }

        assert (cacheDirectory.exists() && cacheDirectory.canWrite());

        path.append("/");
        path.append(StringToolkit.encodeFileName(key));

        try {
            final FileOutputStream outputStream = new FileOutputStream(path.toString());
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
        } catch (final Exception e) {
            L.wtf(BitmapManagerImpl.class, "", e.toString());
        }
    }
}

From source file:com.murrayc.galaxyzoo.app.IconsCache.java

private boolean reloadIcon(final String cssName, final LruCache<String, Bitmap> map) {
    //LruCache throws exceptions on null keys or values.
    if (TextUtils.isEmpty(cssName)) {
        return false;
    }/*from  ww  w  . j  a  v a 2s.  c o m*/

    //Log.info("reloadIcon:" + cssName);

    //Avoid loading and adding it again:
    if (map.get(cssName) != null) {
        return true;
    }

    /*
    Bitmap bitmap = null;
            
    //First get it from the cache, because that would be newer than the bundled asset:
    final String cacheFileUri = getCacheIconFileUri(cssName);
    if (TextUtils.isEmpty(cacheFileUri)) {
    return false;
    }
            
    final File cacheFile = new File(cacheFileUri);
    if (cacheFile.exists()) {
    bitmap = BitmapFactory.decodeFile(cacheFileUri);
    if (bitmap == null) {
        //The file contents are invalid.
        //Maybe the download was incomplete or something else odd happened.
        //Anyway, we should stop trying to use it,
        //And tell the caller about the failure,
        //so we can reload it by reloading and reparsing everything.
        Log.error("IconsCache.reloadIcon(): BitmapFactory.decodeFile() failed for file (now deleting it): ", cacheFileUri);
            
        final File file = new File(cacheFileUri);
        if (!file.delete()) {
            Log.error("IconsCache.reloadIcon(): Failed to delete invalid cache file.");
            return false;
        }
    }
    }
            
    if (bitmap == null) {
    */

    //We bundle the icons with the app,
    //so fall back to that:
    Bitmap bitmap = null;
    final InputStream inputStreamAsset = Utils.openAsset(getContext(), getIconAssetPath(cssName));
    if (inputStreamAsset != null) {
        bitmap = BitmapFactory.decodeStream(inputStreamAsset);
    }
    //}

    if (bitmap == null) {
        Log.error("reloadIcon(): Could not load icon: " + cssName);
        return false;
    }

    map.put(cssName, bitmap);
    return true;
}