首页 > 代码库 > 使用LRU缓存listview图片

使用LRU缓存listview图片

public class ImageviewHolder {
         //设置全局变量
         private ImageView mImageView;
         private String mURL;
         private LruCache<String,Bitmap> mCache;


    public ImageviewHolder(){
        int MaxMem= (int) Runtime.getRuntime().maxMemory();
        int CacheMem=MaxMem/2;
        mCache=new LruCache<String,Bitmap>(CacheMem){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getByteCount();
            }
        };
    }
    private void addBitMapToCache(String url,Bitmap bitmap){
        if(getBitMapFromCache(url)==null){
            mCache.put(url,bitmap);
        }

    }
    private Bitmap getBitMapFromCache(String url){
        return mCache.get(url);

    }
    public void newthread(ImageView imageView, final String url) {
        mImageView = imageView;
        mURL = url;
        Bitmap bitmap=getBitMapFromCache(url);
        if (bitmap==null){
            new MyAsynctask(imageView,mURL).execute(mURL);
        }else {
            imageView.setImageBitmap(bitmap);
        }
        Log.i("i",mCache.size()+"");
        }
    public void showImageBy(ImageView imageView,String mURL)  {
        Bitmap bitmap= getBitMapFromCache(mURL);
        //判断在缓存中的图片是否存在,如果不存在则通过网络下载图片
        if (bitmap==null){
            new MyAsynctask(imageView,mURL).execute(mURL);
        }else {
            imageView.setImageBitmap(bitmap);
        }
    }
    public Bitmap getbitmapstream(String bitmapurl) throws IOException {
        InputStream is=null;
            //指向互联网资源的指针
            URL url=new URL(bitmapurl);
            //打开连接,并强行转换格式
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            //获取输入的数据流
            is=new BufferedInputStream(connection.getInputStream());
            //解析所传入的数据
            Bitmap bitmap= BitmapFactory.decodeStream(is);
            //断开连接
            connection.disconnect();
            return bitmap;
    }
           private class MyAsynctask extends AsyncTask<String,Void,Bitmap>  {
               //通过AsyncTask通过网络下载图片,如果下载的图片不在缓存中则放入缓存中
               private ImageView mImageView;
               private String mURL;
               public MyAsynctask(ImageView imageview,String url){
                   mImageView=imageview;
                   mURL=url;
               }
            @Override
            protected Bitmap doInBackground(String... strings) {
                String url=strings[0];
                //从网络获取图片
                Bitmap bitmap = null;
                try {
                    bitmap = getbitmapstream(url);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //将不在缓存的图片加入缓存
                if (bitmap!=null){
                    addBitMapToCache(url,bitmap);
                }
                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                if (mImageView.getTag().equals(mURL)){
                    mImageView.setImageBitmap(bitmap);
                }
            }
        }
    }
//如果使用new ImageViewHolder方法创建则每次调用都会创建一个新的LRUCache,通过定义全局变量确保只有一个LRUCACHE创建
    public ImageviewHolder mImageViewHolder=new ImageviewHolder();;

 

使用LRU缓存listview图片