Android-图片的缓存实现笔记

来源:互联网 发布:mac我的所有文件 乱 编辑:程序博客网 时间:2024/06/06 04:17

图片的缓存笔记记录

  • 这是处理加载的asset文件的图片
  • 主要代码
public class ImageBuffer {    Context context;    LruCache<String, Bitmap> lruCache;    private static ImageBuffer imageBuffer;    //得到运行的最大内存    int maxMemory = (int) Runtime.getRuntime().maxMemory();    private ImageBuffer(Context context) {        this.context = context;        lruCache = new LruCache<String, Bitmap>(maxMemory / 8)        {            //测量bitmap的大小            @Override            protected int sizeOf(String key, Bitmap value)            {                return value.getRowBytes()*value.getHeight();            }        };    }    //单例模式    public static ImageBuffer getImageBuffer(Context context)    {        if(imageBuffer == null)        {            imageBuffer = new ImageBuffer(context);        }        return imageBuffer;    }    /**     * 获取bitmap     * @param path 在asset中的路径     * @return     */    public Bitmap getBitmap(String path, onImageLoader lis)    {        //首先从缓存中找图片        Bitmap bitmap = lruCache.get(path);        if (bitmap != null)return bitmap;        //如果缓存中没有就到asset中获取        getBitmapInAssets(path, lis);        return null;    }    //从asset文件夹中获取图片    private void getBitmapInAssets(final String path, final onImageLoader lis)    {        new Thread()        {            @Override            public void run() {                AssetManager assetManager = context.getAssets();                try {                    //打开的图片是一个输入流                    InputStream in = assetManager.open(path);                    //将输入流域转换成一个字节数组                    byte[] bytes = getBitmapBytes(in);                    if (bytes != null)                    {                        //对图片压缩处理                        Bitmap bm = scaleBitmap(bytes);                        //添加到缓存                        addBitmapToCache(path, bm);                        lis.onImageLoadeSusscess(bm);                    }                } catch (IOException e) {                    e.printStackTrace();                }            }        }.start();    }    /**     * 将bitmap的添加至缓存     */    public void addBitmapToCache(String key, Bitmap value) {        if (key != null && value != null) {            lruCache.put(key, value);        }    }    /**     * 回调接口     */    public interface onImageLoader{        void onImageLoadeSusscess(Bitmap bitmap);    }    /**     * 获取图片流的字节数组     * @param in     * @return     * @throws IOException      */    public byte[] getBitmapBytes(InputStream in) throws IOException    {        ByteArrayOutputStream out = new ByteArrayOutputStream();        byte[] buffer = new byte[1024];        int len;        while ((len = in.read(buffer)) != -1)        {            out.write(buffer, 0, len);        }        return out.toByteArray();    }    /**     * 对图片进行压缩处理     * @param dataByte 图片的字节数据     * @return     */    public Bitmap scaleBitmap(byte[] dataByte)    {        Options options = new Options();        //每个像素占2个字节,比ARGB_8888降低一半 没有透明度        options.inPreferredConfig = Config.RGB_565;        //让系统能及时回收内存     当内存不足的时候就回收掉        options.inInputShareable = true;        options.inPurgeable = true;        if(dataByte.length>2048){            //对大图片进行压缩,编程原图的1/2            options.inSampleSize = 2;        }        return BitmapFactory.decodeByteArray(dataByte, 0, dataByte.length, options);    }}
0 0
原创粉丝点击