当前位置: 首页>>代码示例>>Java>>正文


Java LruCache.put方法代码示例

本文整理汇总了Java中android.util.LruCache.put方法的典型用法代码示例。如果您正苦于以下问题:Java LruCache.put方法的具体用法?Java LruCache.put怎么用?Java LruCache.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.util.LruCache的用法示例。


在下文中一共展示了LruCache.put方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setValue

import android.util.LruCache; //导入方法依赖的package包/类
/**
 * Puts the given value in the memory cache and persists it in the disk store. If the disk store
 * isn't initialized yet, the disk write operation will block until it's prepared. Any previous
 * values are silently overwritten.
 *
 * @param storeEnabler
 *         The callback that will provide the memory and disk stores.
 * @param key
 *         The key of the value to store.
 * @param value
 *         The value to store.
 *
 * @throws IOException
 *         If the file system operation fails for some reason.
 */
private static void setValue(final RuntimeStoreEnabler storeEnabler, Object key, Object value) throws IOException {
    LruCache<Object, Object> memoryStore = storeEnabler.getMemoryStore();
    if (memoryStore == null) {
        throw new IllegalStateException("You're trying to write content to a closed store.");
    }

    memoryStore.put(key, value);

    // Update disk. Make sure we wait for the disk store to be ready before we start accessing
    // it.
    synchronized (storeEnabler.getDiskStoreLock()) {
        File diskStore = storeEnabler.getDiskStore();
        if (isWritableDirectory(diskStore)) {
            String fileName = getFileName(key);
            File file = new File(diskStore, fileName);
            writeObjectToDisk(file, value);
        }
    }
}
 
开发者ID:podio,项目名称:podio-android,代码行数:35,代码来源:SetRequest.java

示例2: verifyCleanUpRoutine

import android.util.LruCache; //导入方法依赖的package包/类
@Test
public void verifyCleanUpRoutine() {
    LruCache<Integer, WeakReference<Job>> cache = new LruCache<>(20);
    cache.put(1, new WeakReference<>(createJobMock(1)));
    cache.put(2, new WeakReference<Job>(null));

    new JobExecutor().cleanUpRoutine(cache);

    assertThat(cache.size()).isEqualTo(1);
}
 
开发者ID:evernote,项目名称:android-job,代码行数:11,代码来源:JobExecutorTest.java

示例3: updateData

import android.util.LruCache; //导入方法依赖的package包/类
public static synchronized <DATA> void updateData(Class<DATA> clazz, Object index, DATA value) {
    LruCache<Object, DATA> dataMap = clazzDataMap.get(clazz);
    if (dataMap == null) {
        dataMap = new LruCache<>(MAX_COUNT);
        clazzDataMap.put(clazz, dataMap);
    }
    Log.d(TAG, String.format("save %s into %s.", String.valueOf(index), clazz.getSimpleName()));
    Log.d(TAG, "Current Size" + dataMap.size());
    dataMap.put(index, value);
}
 
开发者ID:kifile,项目名称:Cornerstone,代码行数:11,代码来源:SingleData.java

示例4: shouldLru

import android.util.LruCache; //导入方法依赖的package包/类
@Test
public void shouldLru() throws Exception {
  LruCache<Integer, String> lruCache = new LruCache<Integer, String>(2);
  lruCache.put(1, "one");
  lruCache.put(2, "two");
  lruCache.put(3, "three");

  assertThat(lruCache.size()).isEqualTo(2);
  assertThat(lruCache.get(1)).isNull();
  assertThat(lruCache.get(2)).isEqualTo("two");
  assertThat(lruCache.get(3)).isEqualTo("three");
}
 
开发者ID:qx,项目名称:FullRobolectricTestSample,代码行数:13,代码来源:LruTest.java

示例5: getValue

import android.util.LruCache; //导入方法依赖的package包/类
/**
 * Retrieves a value, associated with the given key, from the memory cache. If not found, an
 * attempt to fetch the value from the disk store is made. This method call will block until the
 * disk store is prepared. On success the value will be put in the memory cache. If no value is
 * found neither in the memory, nor on disk, null is returned.
 *
 * @param storeEnabler
 *         The callback that will provide the memory and disk stores.
 * @param key
 *         The key for the value to fetch.
 * @param classOfValue
 *         The {@link Class} template to parse the disk store JSON to.
 *
 * @return The value associated with the key or null if none found.
 *
 * @throws IOException
 *         If reading from disk store failed for some reason.
 * @throws ClassCastException
 *         If the value can't be cast to the requested template type.
 */
@SuppressWarnings("unchecked")
private static final <E> E getValue(RuntimeStoreEnabler storeEnabler, Object key, Class<E> classOfValue) throws IOException, ClassCastException {
    LruCache<Object, Object> memoryStore = storeEnabler.getMemoryStore();
    if (memoryStore == null) {
        throw new IllegalStateException("You're trying to fetch content from a closed store.");
    }

    E value = (E) memoryStore.get(key);

    // If nothing found in the memory cache, try to read from disk.
    if (value == null) {
        // Make sure we wait for the disk store to be ready before we start accessing it.
        synchronized (storeEnabler.getDiskStoreLock()) {
            File diskStore = storeEnabler.getDiskStore();
            if (isReadableDirectory(diskStore) && isValidTemplate(classOfValue)) {
                // Read object from disk...
                String fileName = getFileName(key);
                File file = new File(diskStore, fileName);
                value = readObjectFromDisk(file, classOfValue);

                // ...and also update in memory.
                if (value != null) {
                    memoryStore.put(key, value);
                }
            }
        }
    }

    return value;

}
 
开发者ID:podio,项目名称:podio-android,代码行数:52,代码来源:GetRequest.java

示例6: shotRecyclerView

import android.util.LruCache; //导入方法依赖的package包/类
/**
 * RecyclerView截图
 */
public static Bitmap shotRecyclerView(RecyclerView view, int bgColor, int count) {
    RecyclerView.Adapter adapter = view.getAdapter();
    Bitmap bigBitmap = null;
    if (adapter != null) {
        int size = Math.min(count, 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> bitmapCache = 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) {

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

        bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);
        Canvas bigCanvas = new Canvas(bigBitmap);

        bigCanvas.drawColor(bgColor);

        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 = bitmapCache.get(String.valueOf(i));
            bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);
            iHeight += bitmap.getHeight();
            bitmap.recycle();
        }
    }
    return bigBitmap;
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:56,代码来源:RUtils.java

示例7: DisplayItem

import android.util.LruCache; //导入方法依赖的package包/类
/**
 * Default Constructor
 *
 * @param adapter The adapter for this item
 * @param item The Item
 * @param id   The internal id for the item
 * @param imageCache The thumbnail image cache
 */
public DisplayItem(final DisplayItemAdapter adapter,
                   final Item item,
                   final String id,
                   final LruCache<String, Bitmap> imageCache) {
    mImageCache = imageCache;
    mItem = item;
    mId = id;

    if (hasThumbnail()) {
        final BaseApplication base = (BaseApplication)adapter.getContext().getApplicationContext();
        mGetThumbnailTask = new AsyncTask<Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(final Void... params) {
                final Bitmap foundImage = imageCache.get(mId);
                if (foundImage != null) {
                    return foundImage;
                }
                Log.i("DisplayItem", "Getting thumbnail for " + mId);

                InputStream in = null;
                try {
                    in = base.getOneDriveClient()
                            .getDrive()
                            .getItems(mId)
                            .getThumbnails("0")
                            .getThumbnailSize("small")
                            .getContent()
                            .buildRequest()
                            .get();
                    final Bitmap bitmap = BitmapFactory.decodeStream(in);
                    imageCache.put(mId, bitmap);
                    return bitmap;
                } catch (final Throwable e) {
                    Log.e(getClass().getSimpleName(), "Thumbnail download failure", e);
                    return null;
                } finally {
                    if (in != null) {
                        try {
                            in.close();
                        } catch (final IOException e) {
                            Log.d(getClass().getSimpleName(), "Problem closing thumbnail stream", e);
                        }
                    }
                }
            }

            @Override
            protected void onPostExecute(final Bitmap image) {
                if (image != null) {
                    adapter.notifyDataSetChanged();
                }
            }
        };
        mGetThumbnailTask.execute();
    } else {
        mGetThumbnailTask = null;
    }
}
 
开发者ID:OneDrive,项目名称:onedrive-explorer-android,代码行数:68,代码来源:DisplayItem.java


注:本文中的android.util.LruCache.put方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。