Android懒人加载图片,暧昧点?

时间:2015-12-01 14:11:12

标签: android lazy-loading

我在网上发现了一些延迟加载图片的代码,我在我的应用中使用了它。我有理解代码的麻烦。我已经阅读了很多关于此事的其他帖子,但都没有帮助。我的问题是:

  1. 根据下面的代码,getView方法在哪一点返回视图?我的意思是应用程序首先设置默认图片,然后从url或文件缓存加载实际图像。所以逻辑上getView必须返回两次视图,一次是默认图片,一次是实际图片。这是真的?程序如何?
  2. imageViewReused()方法中会发生什么?每对(imageView,url)都会立即放入地图中,然后插入到进程(DisplayImage)中,但稍后在imageViewReused()中,它会与同一个imageView对中的url进行比较?难道这些总是不一样吗?我知道当滚动listView时,没有创建新的imageView,但是从listView触发的那个会被回收。即使考虑到这一点,我们仍然有一对具有相同的imageView,但新的url将替换为地图中的旧url。那么imageViewReused()中if语句的目的是什么?换句话说,如果省略if语句以使方法始终返回false,该怎么办?我实际上已经完成了这项工作,但在应用程序性能方面没有任何影响!
  3. 快速滚动或反复滚动3次或更多次,应用程序只会崩溃。 LogCat说outOfMemoryException。怎么处理呢?请注意,代码中使用了持有者模式。
  4. 以下是我的代码:

    public class ImageLoader {
    
    MemoryCache memoryCache = new MemoryCache();
    FileCache fileCache;
    private Map<ImageView, String> imageViews = Collections
            .synchronizedMap(new WeakHashMap<ImageView, String>());
    ExecutorService executorService;
    Handler handler = new Handler();
    
    public ImageLoader(Context context) {
        fileCache = new FileCache(context);
        executorService = Executors.newFixedThreadPool(5);
    }
    
    final int stub_id = R.drawable.profile;
    
    public void DisplayImage(String url, ImageView imageView) {
        imageViews.put(imageView, url);
        Bitmap bitmap = memoryCache.get(url);
        if (bitmap != null)
            imageView.setImageBitmap(bitmap);
        else {
            queuePhoto(url, imageView);
            imageView.setImageResource(stub_id);
        }
    }
    
    private void queuePhoto(String url, ImageView imageView) {
        PhotoToLoad p = new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }
    private Bitmap getBitmap(String url) {
        File f = fileCache.getFile(url);
    
        // from SD cache
        Bitmap b = decodeFile(f);
        if (b != null)
            return b;
    
        // from web
        try {
            Bitmap bitmap = null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) imageUrl
                    .openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            conn.disconnect();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Throwable ex) {
            ex.printStackTrace();
            if (ex instanceof OutOfMemoryError)
                memoryCache.clear();
            return null;
        }
    }
    
    // decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f) {
        try {
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            FileInputStream stream1 = new FileInputStream(f);
            BitmapFactory.decodeStream(stream1, null, o);
            stream1.close();
    
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }
    
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            FileInputStream stream2 = new FileInputStream(f);
            Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
            stream2.close();
            return bitmap;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    // Task for the queue
    private class PhotoToLoad {
        public String url;
        public ImageView imageView;
    
        public PhotoToLoad(String u, ImageView i) {
            url = u;
            imageView = i;
        }
    }
    
    class PhotosLoader implements Runnable {
        PhotoToLoad photoToLoad;
    
        PhotosLoader(PhotoToLoad photoToLoad) {
            this.photoToLoad = photoToLoad;
        }
    
        @Override
        public void run() {
            try {
                if (imageViewReused(photoToLoad))
                    return;
                Bitmap bmp = getBitmap(photoToLoad.url);
                memoryCache.put(photoToLoad.url, bmp);
                if (imageViewReused(photoToLoad))
                    return;
                BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
                handler.post(bd);
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }
    
    boolean imageViewReused(PhotoToLoad photoToLoad) {
        String tag = imageViews.get(photoToLoad.imageView);
        if (tag == null || !tag.equals(photoToLoad.url))
            return true;
        return false;
    }
    
    // Used to display bitmap in the UI thread
    class BitmapDisplayer implements Runnable {
        Bitmap bitmap;
        PhotoToLoad photoToLoad;
    
        public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
            bitmap = b;
            photoToLoad = p;
        }
    
        public void run() {
            if (imageViewReused(photoToLoad))
                return;
            if (bitmap != null)
                photoToLoad.imageView.setImageBitmap(bitmap);
            else
                photoToLoad.imageView.setImageResource(stub_id);
        }
    }
    public void clearCache() {
        memoryCache.clear();
        fileCache.clear();
    }
    }
    

1 个答案:

答案 0 :(得分:2)

我建议使用fresco(甚至支持关闭堆图像存储),GlidePicasso,这些都是高度优化的Android图像加载库。我一般反对包括一个图书馆的所有东西,但对于图像加载你想要快速和无bug。一个简单的课程不能提供这个