简单的基于LruCache的图片加载器

本文介绍了一个基于LruCache的简单图片加载器实现,用于加载本地图片缩略图。该加载器采用异步任务处理图片加载,并实现了内存缓存机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码片段记录:
该代码段引用自google官方的support-v7 demo,是使用LruCache做的一个简单的二级缓存【disk/memory】的图片加载器——因为该ImageLoader是用来加载本地图片缩略图的,不涉及网络请求。

package com.example.android.supportv7.graphics;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.v4.graphics.BitmapCompat;
import android.support.v4.os.AsyncTaskCompat;
import android.support.v4.util.LruCache;
import android.widget.ImageView;

/**
 * A very naive lazily implemented image loader. Do not use this in production code.
 */
class ImageLoader {

    /**
     * A LruCache used to store images which has a maximum size of 10% of the maximum heap size.
     */
    private static final BitmapCache CACHE = new BitmapCache(
            Math.round(Runtime.getRuntime().maxMemory() / 10));

    private ImageLoader() {
    }

    interface Listener {
        void onImageLoaded(Bitmap bitmap);
    }

    static void loadMediaStoreThumbnail(final ImageView imageView,
            final long id,
            final Listener listener) {

        final Bitmap cachedValue = CACHE.get(id);
        if (cachedValue != null) {
            // If the image is already in the cache, display the image,
            // call the listener now and return
            imageView.setImageBitmap(cachedValue);
            if (listener != null) {
                listener.onImageLoaded(cachedValue);
            }
            return;
        }

        AsyncTaskCompat.executeParallel(new AsyncTask<Void, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(Void... params) {
                return MediaStore.Images.Thumbnails.getThumbnail(
                        imageView.getContext().getContentResolver(),
                        id,
                        MediaStore.Images.Thumbnails.MINI_KIND,
                        null);
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);

                if (bitmap != null) {
                    // Add the image to the memory cache first
                    CACHE.put(id, bitmap);

                    if (listener != null) {
                        listener.onImageLoaded(bitmap);
                    }
                }
            }
        });
    }

    /**
     * A simple cache implementation for {@link android.graphics.Bitmap} instances which uses
     * {@link android.support.v4.util.LruCache}.
     */
    private static class BitmapCache extends LruCache<Long, Bitmap> {
        BitmapCache(int maxSize) {
            super(maxSize);
        }

        @Override
        protected int sizeOf(Long key, Bitmap value) {
            return BitmapCompat.getAllocationByteCount(value);
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值