Android网络图片加载优化

网络图片加载优化

比如使用淘宝浏览产品的时候(大部分应用也是如此),就会发现每次下拉产品目录进行更新的时候,都会出现对应的Item的时候,才开始从网络下载并加载图片。
taobao加载

可以看到宝贝图片下拉刷新的时候,图片加载是实时从网络下载的。即使在Wifi的网络环境下,加载图片也是有比较大的延迟。

假设我们浏览每屏宝贝需要2s的时间(人眼对于淘宝搜索的宝贝其实过滤速度非常快)。如果每一屏页面需要1s才可以完全加载完图片,则如果浏览10屏的宝贝,就会需要30s。如果加载图片几乎不需要时间,则只需要20s。这个节约比例是很大的。如果不是使用wifi而是移动网络,则加载时间可能会更长。这种情况不单单是淘宝,很多带有图片的app都会出现这种图片需要很久加载的情况。

而解决这种问题的思路其实很简单也很直观,那就是在wifi或者用户打开设置的情况下,提前加载一屏的图片,保存在本地硬盘(不是内存)。如果浏览到时则从本地加载。

加载的优化基于上一篇文章:
使用Android-Universal-Image-Loader加载网络图片
http://blog.csdn.net/chen52671/article/details/44680765

使用Android-Universal-Image-Loader是为了简化这个例子,如果你有更好地网络图片下载和内存缓存,硬盘缓存的工具,道理是一样的。

实现思路:
1,判断网络环境是否为WiFi。
2,如果是则开启预缓存,当在Page1时,下载Page2的图片到本地;下拉到paga2时,图片直接从本地读取到内存并显示,同时下载Page3到本地。以此类推

让Android-Universal-Image-Loader只下载

ImageLoader主要使用displayImage来加载某URI的图片到ImageView。还有个方法是loadImage()——用来异步加载图片:其也是通过调用displayImage来操作的,不过传递进去的imageAware(可以当ImageView理解)是一个空实现,当Bitmap加载完毕后,返回一个回调。
但是都没法满足我们的要求:图片流只下载至本地硬盘缓存,但是不解析成Bitmap。更不要提将解析好的Bitmap加载到内存缓存中。
借用一张ImageLoader的displayImage流程图:

display-image-flow-chart
图片来自Android Universal Image Loader 源码分析
Android网络图片加载优化_第1张图片

为了实现只下载,不加载。需要对Android-Universal-Image-Loader做一下变动(对不住了~~)
1,ImageLoader类添加一个downloadImage方法,和displayImage类似,同样要遵循已有的配置,也要判断是不是在本地硬盘缓存已经有该图片。
2,如果DisplayImageOptions根本就没配置cacheOnDisk,那就对不住了,不下载。

修改如下:

增加一个DownloadTask

package com.nostra13.universalimageloader.core;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;


import android.os.Handler;
import android.util.Log;

import com.nostra13.universalimageloader.core.decode.ImageDecoder;
import com.nostra13.universalimageloader.core.download.ImageDownloader;

import com.nostra13.universalimageloader.core.listener.DownloadListener;

import com.nostra13.universalimageloader.utils.IoUtils;

public class DownloadTask implements Runnable, IoUtils.CopyListener {
    private String Tag = "DownloadTask";
    private final ImageLoaderEngine engine;
    private final ImageDownloadInfo imageDownloadInfo;
    private final Handler handler;
    // Helper references
    private final ImageLoaderConfiguration configuration;
    private final ImageDownloader downloader;
    private final ImageDownloader networkDeniedDownloader;
    private final ImageDownloader slowNetworkDownloader;
    private final ImageDecoder decoder;
    final DownloadListener listener;
    final String uri;

    final DisplayImageOptions options;

    private final boolean syncLoading;

    public DownloadTask(ImageLoaderEngine engine,
            ImageDownloadInfo imageDownloadInfo, Handler handler) {
        this.engine = engine;
        this.imageDownloadInfo = imageDownloadInfo;
        this.handler = handler;

        configuration = engine.configuration;
        downloader = configuration.downloader;
        networkDeniedDownloader = configuration.networkDeniedDownloader;
        slowNetworkDownloader = configuration.slowNetworkDownloader;
        decoder = configuration.decoder;
        uri = imageDownloadInfo.uri;
        listener = imageDownloadInfo.listener;
        options = imageDownloadInfo.options;
        syncLoading = options.isSyncLoading();
    }

    @Override
    public void run() {
        if (waitIfPaused())
            return;

        ReentrantLock downloadLock = engine.getLockForUri(uri);

        if (downloadLock.isLocked()) {
            Log.d(Tag, "locked");
        }

        downloadLock.lock();
        try {
            File imageFile = configuration.diskCache.get(uri);
            if (imageFile != null && imageFile.exists()
                    && imageFile.length() > 0) {
                return;// 文件存在,不用忙活了
            }
            if (options.isCacheOnDisk()) {
                if(tryCacheImageOnDisk()){
                    listener.onDownloadComplete(uri);
                }
            }
            checkTaskInterrupted();
        } catch (TaskCancelledException e) {
            fireCancelEvent();
            return;
        } finally {
            downloadLock.unlock();
        }

    }

    private boolean waitIfPaused() {
        AtomicBoolean pause = engine.getPause();
        if (pause.get()) {
            synchronized (engine.getPauseLock()) {
                if (pause.get()) {
                    try {
                        engine.getPauseLock().wait();
                    } catch (InterruptedException e) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    String getLoadingUri() {
        return uri;
    }

    private void checkTaskInterrupted() throws TaskCancelledException {
        if (isTaskInterrupted()) {
            throw new TaskCancelledException();
        }
    }

    /**
     * @return true - if current task was interrupted; false -
     *         otherwise
     */
    private boolean isTaskInterrupted() {
        if (Thread.interrupted()) {
            Log.d(Tag, "Thread interrupted");
            return true;
        }
        return false;
    }

    static void runTask(Runnable r, boolean sync, Handler handler,
            ImageLoaderEngine engine) {
        if (sync) {
            r.run();
        } else if (handler == null) {
            engine.fireCallback(r);
        } else {
            handler.post(r);
        }
    }

    private void fireCancelEvent() {
        if (syncLoading || isTaskInterrupted())
            return;
        Runnable r = new Runnable() {
            @Override
            public void run() {
                listener.onDownloadCancelled(uri);
            }
        };
        runTask(r, false, handler, engine);
    }

    private boolean tryCacheImageOnDisk() throws TaskCancelledException {

        boolean loaded;
        try {
            loaded = downloadImage();
            // 暂时不resize
        } catch (IOException e) {
            loaded = false;
        }
        return loaded;
    }

    private boolean downloadImage() throws IOException {
        InputStream is = getDownloader().getStream(uri,
                options.getExtraForDownloader());
        if (is == null) {

            return false;
        } else {
            try {
                return configuration.diskCache.save(uri, is, this);
            } finally {
                IoUtils.closeSilently(is);
            }
        }
    }

    private ImageDownloader getDownloader() {
        ImageDownloader d;
        if (engine.isNetworkDenied()) {
            d = networkDeniedDownloader;
        } else if (engine.isSlowNetwork()) {
            d = slowNetworkDownloader;
        } else {
            d = downloader;
        }
        return d;
    }

    class TaskCancelledException extends Exception {
    }

    @Override
    public boolean onBytesCopied(int current, int total) {
        // TODO Auto-generated method stub
        return true;
    }
}

增加一个ImageDownloadInfo

package com.nostra13.universalimageloader.core;

import java.util.concurrent.locks.ReentrantLock;

import com.nostra13.universalimageloader.core.listener.DownloadListener;

public class ImageDownloadInfo {

    final String uri;
    final DownloadListener listener;
    final DisplayImageOptions options;

    final ReentrantLock downloadLock;

    public ImageDownloadInfo(String uri ,DisplayImageOptions options,DownloadListener listener,
            ReentrantLock downloadLock) {
        this.uri = uri;
        this.listener=listener;
        this.options = options;
        this.downloadLock = downloadLock;

    }
}

增加一个DownloadListener监听器

package com.nostra13.universalimageloader.core.listener;

public interface  DownloadListener{
    void OnDownloadStart(String imageUri);

    void onDownloadFailed(String imageUri);

    void onDownloadComplete(String imageUri);

    void onDownloadCancelled(String imageUri);
}

ImageLoader修改

/*
     * 
     * @param uri Image URI (i.e. "http://site.com/image.png",
     * "file:///mnt/sdcard/image.png")
     * 
     * @param listener 图片下载的监听器
     */
    public void downloadImage(String uri, DisplayImageOptions options,
            DownloadListener listener) {
        checkConfiguration();
        if (listener == null) {
            listener = new DownloadListener() {
                @Override
                public void onDownloadFailed(String imageUri) {
                }

                @Override
                public void onDownloadComplete(String imageUri) {
                }

                @Override
                public void onDownloadCancelled(String imageUri) {
                }

                @Override
                public void OnDownloadStart(String imageUri) {

                }
            };
        }
        if (options == null) {
            options = configuration.defaultDisplayImageOptions;
        }
        if(!options.isCacheOnDisk()) return;
        listener.OnDownloadStart(uri);
        if (TextUtils.isEmpty(uri)) {
            listener.onDownloadFailed(uri);
            return;
        }

        ImageDownloadInfo imageDownloadInfo = new ImageDownloadInfo(uri,options,listener,engine.getLockForUri(uri));
        DownloadTask downloadTask = new DownloadTask(
                engine, imageDownloadInfo,defineHandler(options));
        if (options.isSyncLoading()) {
            downloadTask.run();
        } else {
            engine.submit(downloadTask);
        }

    }

ImageLoaderEngine中添加了一个submit方法

    //for downLoad tast
    void submit(final DownloadTask task) {
        taskDistributor.execute(new Runnable() {
            @Override
            public void run() {
                File image = configuration.diskCache.get(task.getLoadingUri());
                boolean isImageCachedOnDisk = image != null && image.exists();
                initExecutorsIfNeed();
                if (isImageCachedOnDisk) {
                    //do nothing.casue we have the image files already!
                } else {
                    taskExecutor.execute(task);
                }
            }
        });
    }

由于实现的比较仓促,还有很多细节问题需要实现:
1,没有实现比如文件保存时没有resize图片大小。
2,还有如果刚刚添加了图片下载任务,这时候下拉刷新,就会图片就会增加一个加载并显示的任务,这时如果下载任务没完成,应该是取消下载任务(cancelDownloadTask),进行图片显示任务。更完善一点的是等待图片下载任务完成,再进行加载任务。这些细节就没有考虑了,所以进行时可能会有些问题。

优化

这个工程是基于上一篇文章的基础上修改优化的。http://blog.csdn.net/chen52671/article/details/44680765
原效果图如下:

ImageLoader加载

优化前

Android网络图片加载优化_第2张图片
可以看出一个Page大概加载15张图片。如果是每个Item的getView时,触发item position+15的那个Item的图片的下载任务即可。

该效果图是对taobao加载宝贝图片的一个还原,可以看到起滑动刷新的时候,图片也是需要联网下载并加载,同样存在一定的延迟效果。

优化后:

优化后的ImageLoader加载

效果图看出,图片翻页时的加载速度明显提升,并且由于是直接缓存在本地硬盘,不会造成明显的内存占用,从一定程度上解决了翻页时的网络图片加载缓慢问题

优化修改主要在MyAdapter类。使用的ImageLoader是前面修改的版本。

MyAdapter

package com.example.gridpic;

import java.util.List;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.listener.DownloadListener;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;
import android.widget.ImageView;

public class MyAdapter extends BaseAdapter {
    List imageList;
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    protected DisplayImageOptions options;
    private LayoutInflater mInflater;
    public Context context;

    public MyAdapter(Context context, List imageList) {
        super();
        this.imageList = imageList;
        this.context = context;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher)
                .showImageForEmptyUri(R.drawable.ic_launcher)
                .showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
                .cacheOnDisk(true).bitmapConfig(Bitmap.Config.RGB_565).build();

        this.mInflater = LayoutInflater.from(context);
        // 初始化ImageLoader
        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(
                context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);
        config.writeDebugLogs(); // Remove for release app
        ImageLoader.getInstance().init(config.build());
    }

    @Override
    public int getCount() {
        return imageList.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            view = mInflater.inflate(R.layout.picitem, null);
            holder = new ViewHolder();
            holder.image = (ImageView) view.findViewById(R.id.imageView1);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        imageLoader
                .displayImage(imageList.get(position), holder.image, options);
        //判断如果是Wifi环境,则采用Disc预加载策略
        if (isWifi()) {
            final int cachePosition = Math.min(position + 15,
                    imageList.size() - 1);
            imageLoader.downloadImage(imageList.get(cachePosition), options,
                    new DownloadListener() {

                        @Override
                        public void onDownloadFailed(String arg0) {
                        }

                        @Override
                        public void onDownloadComplete(String arg0) {
                            Log.e("whatever", "Cache Image:" + cachePosition
                                    + ". url=" + imageList.get(cachePosition));
                        }

                        @Override
                        public void onDownloadCancelled(String arg0) {
                        }

                        @Override
                        public void OnDownloadStart(String arg0) {

                        }
                    });
        }

        Log.e("whatever",
                "Get Image:" + position + ". url=" + imageList.get(position));
        return view;
    }

    public boolean isWifi() {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
        if (activeNetInfo != null
                && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
        return false;
    }

    final class ViewHolder {

        public ImageView image;

    }

}

加载图片的Log

从log中也能看出图片加载时的策略能够看出,图片的加载,下载的确是按照之前设想的思路进行的。

03-28 19:48:13.300: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=1
03-28 19:48:14.220: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=2
03-28 19:48:14.860: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=3
03-28 19:48:15.640: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=4
03-28 19:48:17.390: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=5
03-28 19:48:17.880: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=6
03-28 19:48:18.640: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=7
03-28 19:48:19.170: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=8
03-28 19:48:20.500: E/whatever(5315): http://www.juzimi.com/meitumeiju?page=9
03-28 19:48:21.060: E/whatever(5315): URL ready
03-28 19:48:21.160: E/libEGL(5315): call to OpenGL ES API with no current context (logged once per thread)
03-28 19:48:21.160: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:21.170: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:21.170: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:21.170: E/whatever(5315): Get Image:1. url=http://file.juzimi.com/weibopic/jizxmd1.jpg
03-28 19:48:21.180: E/whatever(5315): Get Image:2. url=http://file.juzimi.com/weibopic/jrzdmd7.jpg
03-28 19:48:21.180: E/whatever(5315): Get Image:3. url=http://file.juzimi.com/weibopic/jozome2.jpg
03-28 19:48:21.180: E/whatever(5315): Get Image:4. url=http://file.juzimi.com/weibopic/jpzlmu7.jpg
03-28 19:48:21.180: E/whatever(5315): Get Image:5. url=http://file.juzimi.com/weibopic/jezimr6.jpg
03-28 19:48:21.190: E/whatever(5315): Get Image:6. url=http://file.juzimi.com/weibopic/jezemp2.jpg
03-28 19:48:21.190: E/whatever(5315): Get Image:7. url=http://file.juzimi.com/weibopic/jrzlmx2.jpg
03-28 19:48:21.190: E/whatever(5315): Get Image:8. url=http://file.juzimi.com/weibopic/jrzomd2.jpg
03-28 19:48:21.190: E/whatever(5315): Get Image:9. url=http://file.juzimi.com/weibopic/jizpml4.jpg
03-28 19:48:21.200: E/whatever(5315): Get Image:10. url=http://file.juzimi.com/weibopic/jezlmd1.jpg
03-28 19:48:21.200: E/whatever(5315): Get Image:11. url=http://file.juzimi.com/weibopic/jiziml3.jpg
03-28 19:48:21.200: E/whatever(5315): Get Image:12. url=http://file.juzimi.com/weibopic/jlzdmd3.jpg
03-28 19:48:21.200: E/whatever(5315): Get Image:13. url=http://file.juzimi.com/weibopic/jxzlmu2.jpg
03-28 19:48:21.210: E/whatever(5315): Get Image:14. url=http://file.juzimi.com/weibopic/jdzpmx2.jpg
03-28 19:48:22.090: E/whatever(5315): Cache Image:15. url=http://file.juzimi.com/weibopic/jezpma2.jpg
03-28 19:48:22.300: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:22.300: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:22.350: E/whatever(5315): Cache Image:29. url=http://file.juzimi.com/weibopic/juzlma7.jpg
03-28 19:48:22.440: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:22.440: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:23.120: E/whatever(5315): Cache Image:28. url=http://file.juzimi.com/weibopic/jpzoma2.jpg
03-28 19:48:23.370: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:23.380: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:23.790: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:23.790: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:23.890: E/whatever(5315): Cache Image:27. url=http://file.juzimi.com/weibopic/jazlml6.jpg
03-28 19:48:23.990: E/whatever(5315): Cache Image:26. url=http://file.juzimi.com/weibopic/jpzdmr2.jpg
03-28 19:48:24.270: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:24.270: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:24.270: E/whatever(5315): Cache Image:25. url=http://file.juzimi.com/weibopic/jezrmu5.jpg
03-28 19:48:24.500: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:24.510: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:24.600: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:24.610: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:24.770: E/whatever(5315): Cache Image:23. url=http://file.juzimi.com/weibopic/jazxmr4.jpg
03-28 19:48:25.510: E/whatever(5315): Cache Image:22. url=http://file.juzimi.com/weibopic/jozdme6.jpg
03-28 19:48:25.560: E/whatever(5315): Cache Image:24. url=http://file.juzimi.com/weibopic/jlzamd7.jpg
03-28 19:48:26.300: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:26.300: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:26.410: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:26.410: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:26.440: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:26.440: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:26.500: E/whatever(5315): Cache Image:20. url=http://file.juzimi.com/weibopic/juzdmd7.jpg
03-28 19:48:27.780: E/whatever(5315): Cache Image:21. url=http://file.juzimi.com/weibopic/jizdma2.jpg
03-28 19:48:28.030: E/whatever(5315): Cache Image:19. url=http://file.juzimi.com/weibopic/jizoma3.jpg
03-28 19:48:28.420: E/whatever(5315): Cache Image:18. url=http://file.juzimi.com/weibopic/juzlmx.jpg
03-28 19:48:28.420: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:28.420: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:28.800: E/whatever(5315): Cache Image:17. url=http://file.juzimi.com/weibopic/jpzxmr5.jpg
03-28 19:48:28.870: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:28.870: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:29.520: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:29.520: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:29.790: E/whatever(5315): Cache Image:16. url=http://file.juzimi.com/weibopic/jrzlmp2.jpg
03-28 19:48:30.390: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:30.390: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:30.490: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:30.490: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:32.540: E/whatever(5315): Get Image:15. url=http://file.juzimi.com/weibopic/jezpma2.jpg
03-28 19:48:32.540: E/whatever(5315): Get Image:16. url=http://file.juzimi.com/weibopic/jrzlmp2.jpg
03-28 19:48:32.550: E/whatever(5315): Get Image:17. url=http://file.juzimi.com/weibopic/jpzxmr5.jpg
03-28 19:48:32.640: E/whatever(5315): Get Image:18. url=http://file.juzimi.com/weibopic/juzlmx.jpg
03-28 19:48:32.640: E/whatever(5315): Get Image:19. url=http://file.juzimi.com/weibopic/jizoma3.jpg
03-28 19:48:32.650: E/whatever(5315): Get Image:20. url=http://file.juzimi.com/weibopic/juzdmd7.jpg
03-28 19:48:32.650: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:32.680: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:32.740: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:32.740: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:32.750: E/whatever(5315): Get Image:21. url=http://file.juzimi.com/weibopic/jizdma2.jpg
03-28 19:48:32.750: E/whatever(5315): Get Image:22. url=http://file.juzimi.com/weibopic/jozdme6.jpg
03-28 19:48:32.760: E/whatever(5315): Get Image:23. url=http://file.juzimi.com/weibopic/jazxmr4.jpg
03-28 19:48:32.760: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:32.760: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:32.890: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:32.900: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:33.280: E/whatever(5315): Cache Image:32. url=http://file.juzimi.com/weibopic/jpzrmp2.jpg
03-28 19:48:33.640: E/whatever(5315): Cache Image:31. url=http://file.juzimi.com/weibopic/jizomd2.jpg
03-28 19:48:34.080: E/whatever(5315): Cache Image:37. url=http://file.juzimi.com/weibopic/jazlmu4.jpg
03-28 19:48:34.100: E/whatever(5315): Cache Image:30. url=http://file.juzimi.com/weibopic/jxzimu1.jpg
03-28 19:48:34.110: E/whatever(5315): Get Image:24. url=http://file.juzimi.com/weibopic/jlzamd7.jpg
03-28 19:48:34.110: E/whatever(5315): Get Image:25. url=http://file.juzimi.com/weibopic/jezrmu5.jpg
03-28 19:48:34.110: E/whatever(5315): Get Image:26. url=http://file.juzimi.com/weibopic/jpzdmr2.jpg
03-28 19:48:34.190: E/whatever(5315): Get Image:27. url=http://file.juzimi.com/weibopic/jazlml6.jpg
03-28 19:48:34.190: E/whatever(5315): Get Image:28. url=http://file.juzimi.com/weibopic/jpzoma2.jpg
03-28 19:48:34.200: E/whatever(5315): Get Image:29. url=http://file.juzimi.com/weibopic/juzlma7.jpg
03-28 19:48:34.260: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.290: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.300: E/whatever(5315): Cache Image:38. url=http://file.juzimi.com/weibopic/jpzrmr5.jpg
03-28 19:48:34.300: E/whatever(5315): Cache Image:36. url=http://file.juzimi.com/weibopic/juzpma3.jpg
03-28 19:48:34.310: E/whatever(5315): Get Image:30. url=http://file.juzimi.com/weibopic/jxzimu1.jpg
03-28 19:48:34.310: E/whatever(5315): Get Image:31. url=http://file.juzimi.com/weibopic/jizomd2.jpg
03-28 19:48:34.310: E/whatever(5315): Get Image:32. url=http://file.juzimi.com/weibopic/jpzrmp2.jpg
03-28 19:48:34.320: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.320: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.400: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.430: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.440: E/whatever(5315): Cache Image:35. url=http://file.juzimi.com/weibopic/jozdmx.jpg
03-28 19:48:34.450: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.450: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.480: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.480: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:34.480: E/whatever(5315): Cache Image:43. url=http://file.juzimi.com/weibopic/juzpml2.jpg
03-28 19:48:34.930: E/whatever(5315): Cache Image:46. url=http://file.juzimi.com/weibopic/jpzomr5.jpg
03-28 19:48:35.090: E/whatever(5315): Cache Image:44. url=http://file.juzimi.com/weibopic/jozxml1.jpg
03-28 19:48:35.530: E/whatever(5315): Cache Image:47. url=http://file.juzimi.com/weibopic/jozamp7.jpg
03-28 19:48:35.550: E/whatever(5315): Get Image:33. url=http://file.juzimi.com/weibopic/jpzomr.jpg
03-28 19:48:35.560: E/whatever(5315): Get Image:34. url=http://file.juzimi.com/weibopic/juzomr5.jpg
03-28 19:48:35.560: E/whatever(5315): Get Image:35. url=http://file.juzimi.com/weibopic/jozdmx.jpg
03-28 19:48:35.610: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.620: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.640: E/whatever(5315): Get Image:36. url=http://file.juzimi.com/weibopic/juzpma3.jpg
03-28 19:48:35.640: E/whatever(5315): Get Image:37. url=http://file.juzimi.com/weibopic/jazlmu4.jpg
03-28 19:48:35.640: E/whatever(5315): Get Image:38. url=http://file.juzimi.com/weibopic/jpzrmr5.jpg
03-28 19:48:35.720: E/whatever(5315): Get Image:39. url=http://file.juzimi.com/weibopic/jxzlmx5.jpg
03-28 19:48:35.720: E/whatever(5315): Get Image:40. url=http://file.juzimi.com/weibopic/jezlmo3.jpg
03-28 19:48:35.720: E/whatever(5315): Get Image:41. url=http://file.juzimi.com/weibopic/jozeml6.jpg
03-28 19:48:35.740: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.740: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.790: E/whatever(5315): Get Image:42. url=http://file.juzimi.com/weibopic/jlzomi.jpg
03-28 19:48:35.800: E/whatever(5315): Get Image:43. url=http://file.juzimi.com/weibopic/juzpml2.jpg
03-28 19:48:35.800: E/whatever(5315): Get Image:44. url=http://file.juzimi.com/weibopic/jozxml1.jpg
03-28 19:48:35.830: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.830: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.880: E/whatever(5315): Cache Image:45. url=http://file.juzimi.com/weibopic/jdzama3.jpg
03-28 19:48:35.890: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.890: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.910: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:35.910: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:36.120: E/whatever(5315): Cache Image:42. url=http://file.juzimi.com/weibopic/jlzomi.jpg
03-28 19:48:36.180: E/whatever(5315): Cache Image:59. url=http://file.juzimi.com/weibopic/juzxmo7.jpg
03-28 19:48:36.230: E/whatever(5315): Cache Image:41. url=http://file.juzimi.com/weibopic/jozeml6.jpg
03-28 19:48:36.320: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:36.320: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:36.490: E/whatever(5315): Cache Image:58. url=http://file.juzimi.com/weibopic/jrzome.jpg
03-28 19:48:36.810: E/whatever(5315): Cache Image:57. url=http://file.juzimi.com/weibopic/jxzomr3.jpg
03-28 19:48:36.910: E/whatever(5315): Cache Image:54. url=http://file.juzimi.com/weibopic/jizemr.jpg
03-28 19:48:36.940: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:36.940: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:36.970: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:36.970: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:37.170: E/whatever(5315): Cache Image:56. url=http://file.juzimi.com/weibopic/jezomx5.jpg
03-28 19:48:37.240: E/whatever(5315): Cache Image:55. url=http://file.juzimi.com/weibopic/jezdmu7.jpg
03-28 19:48:37.600: E/whatever(5315): Cache Image:53. url=http://file.juzimi.com/weibopic/jrzomr3.jpg
03-28 19:48:37.890: E/whatever(5315): Cache Image:52. url=http://file.juzimi.com/weibopic/jozomr7.jpg
03-28 19:48:38.560: E/whatever(5315): Cache Image:51. url=http://file.juzimi.com/weibopic/jozume4.jpg
03-28 19:48:38.590: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:38.590: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:38.870: E/whatever(5315): Cache Image:50. url=http://file.juzimi.com/weibopic/jazrmp6.jpg
03-28 19:48:38.900: E/whatever(5315): Cache Image:49. url=http://file.juzimi.com/weibopic/jrzumi2.jpg
03-28 19:48:38.970: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:38.970: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:39.120: E/whatever(5315): Cache Image:48. url=http://file.juzimi.com/weibopic/jizeme7.jpg
03-28 19:48:39.140: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:39.140: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:40.150: E/whatever(5315): Get Image:45. url=http://file.juzimi.com/weibopic/jdzama3.jpg
03-28 19:48:40.160: E/whatever(5315): Get Image:46. url=http://file.juzimi.com/weibopic/jpzomr5.jpg
03-28 19:48:40.160: E/whatever(5315): Get Image:47. url=http://file.juzimi.com/weibopic/jozamp7.jpg
03-28 19:48:40.250: E/whatever(5315): Get Image:48. url=http://file.juzimi.com/weibopic/jizeme7.jpg
03-28 19:48:40.260: E/whatever(5315): Get Image:49. url=http://file.juzimi.com/weibopic/jrzumi2.jpg
03-28 19:48:40.260: E/whatever(5315): Get Image:50. url=http://file.juzimi.com/weibopic/jazrmp6.jpg
03-28 19:48:40.330: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:40.330: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:40.380: E/whatever(5315): Get Image:51. url=http://file.juzimi.com/weibopic/jozume4.jpg
03-28 19:48:40.380: E/whatever(5315): Get Image:52. url=http://file.juzimi.com/weibopic/jozomr7.jpg
03-28 19:48:40.380: E/whatever(5315): Get Image:53. url=http://file.juzimi.com/weibopic/jrzomr3.jpg
03-28 19:48:40.390: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:40.390: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:40.450: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:40.460: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:40.460: E/whatever(5315): Cache Image:61. url=http://file.juzimi.com/weibopic/jezema5.jpg
03-28 19:48:40.520: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:40.520: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:41.150: E/whatever(5315): Cache Image:68. url=http://file.juzimi.com/weibopic/jlzoml.jpg
03-28 19:48:41.180: E/whatever(5315): Cache Image:60. url=http://file.juzimi.com/weibopic/juzxmi4.jpg
03-28 19:48:41.540: E/whatever(5315): Get Image:54. url=http://file.juzimi.com/weibopic/jizemr.jpg
03-28 19:48:41.540: E/whatever(5315): Get Image:55. url=http://file.juzimi.com/weibopic/jezdmu7.jpg
03-28 19:48:41.540: E/whatever(5315): Get Image:56. url=http://file.juzimi.com/weibopic/jezomx5.jpg
03-28 19:48:41.590: E/whatever(5315): Get Image:57. url=http://file.juzimi.com/weibopic/jxzomr3.jpg
03-28 19:48:41.590: E/whatever(5315): Get Image:58. url=http://file.juzimi.com/weibopic/jrzome.jpg
03-28 19:48:41.600: E/whatever(5315): Get Image:59. url=http://file.juzimi.com/weibopic/juzxmo7.jpg
03-28 19:48:41.600: E/whatever(5315): Cache Image:66. url=http://file.juzimi.com/weibopic/jezumu3.jpg
03-28 19:48:41.640: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:41.640: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:41.720: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:41.720: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:41.740: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:41.740: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:41.760: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:41.760: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:42.670: E/whatever(5315): Cache Image:73. url=http://file.juzimi.com/weibopic/jazxmd6.jpg
03-28 19:48:42.770: E/whatever(5315): Get Image:60. url=http://file.juzimi.com/weibopic/juzxmi4.jpg
03-28 19:48:42.770: E/whatever(5315): Get Image:61. url=http://file.juzimi.com/weibopic/jezema5.jpg
03-28 19:48:42.770: E/whatever(5315): Cache Image:62. url=http://file.juzimi.com/weibopic/jlzrmd4.jpg
03-28 19:48:42.780: E/whatever(5315): Get Image:62. url=http://file.juzimi.com/weibopic/jlzrmd4.jpg
03-28 19:48:42.860: E/whatever(5315): Get Image:63. url=http://file.juzimi.com/weibopic/jxzomo6.jpg
03-28 19:48:42.860: E/whatever(5315): Get Image:64. url=http://file.juzimi.com/weibopic/juzpmi.jpg
03-28 19:48:42.860: E/whatever(5315): Get Image:65. url=http://file.juzimi.com/weibopic/jizemx1.jpg
03-28 19:48:42.880: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:42.880: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:42.880: E/whatever(5315): Cache Image:67. url=http://file.juzimi.com/weibopic/jrzumi7.jpg
03-28 19:48:42.900: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:42.900: E/whatever(5315): Get Image:0. url=http://file.juzimi.com/weibopic/jlzumo3.jpg
03-28 19:48:42.940: E/whatever(5315): Get Image:66. url=http://file.juzimi.com/weibopic/jezumu3.jpg
03-28 19:48:42.940: E/whatever(5315): Get Image:67. url=http://file.juzimi.com/weibopic/jrzumi7.jpg
03-28 19:48:42.940: E/whatever(5315): Get Image:68. url=http://file.juzimi.com/weibopic/jlzoml.jpg

后记

根据效果图可以看出,在浏览时停留几秒,然后滑动页面,加载速度的确会快了些。不过除了上面说到的ImageLoader存在的问题外。图片加载时还存在如下细节问题:

1,Page1的15张图片加载时,同时第16-30张图片也会加入到下载队列,这就造成了第一页的15张图片加载缓慢。
解决方法:方法1:修改ImageLoader让下载任务的优先级降低。方法2:在工程中当前显示的图片任务都显示完毕后,在完成的回调中再加入下载任务。
2,当图片滑动过快的时候,比如从第1页,直接滑到了第5页,那234页在工程中还是要一步步的下载。而为了用户体验更好,那234页是没必要下载,或者最晚下载的。能想到的比较好的方法就是,显示到哪些item了,那就计算出之前的哪些item已经不在屏幕内了,然后cancel掉下载的任务。或者改变那些下载任务的优先级。
3, 不论是图片显示,还是图片下载,添加任务的时候是按照图片顺序添加的,完成却是按照逆序完成的。原因是Imageloader设定成了tasksProcessingOrder(QueueProcessingType.LIFO),不过不影响实际效果,根据项目需求设定吧。


以上为抛砖引玉,仅供参考 , 欢迎讨论。

你可能感兴趣的:(Android应用)