使用UniversalImageDownloader不顺畅的ListView滚动 [英] ListView scrolling using UniversalImageDownloader not smooth

查看:104
本文介绍了使用UniversalImageDownloader不顺畅的ListView滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的含有图像的ListView控件。这些图像被从适配器内部的因特网加载。因此,我现在用的是 UniversalImageDownloader

不幸的是,ListView控件的滚动滞后的时间很短,只要我向下滚动新内容已被下载的地方。

我喜欢的ListView滚动完美流畅,但图像的加载实际上可以预期的行为,当然需要更多的时间 - 这应该不会影响滚动的平滑度

此外,我滚动备份滞后出现为好。看来,如果图像是不正确缓存。

也许我ImageLoader的选项是设置错了?

我是不是做错了什么在我的适配器?

ListView控件包含约20-30图像尺寸640X320(约150KB)

下面你可以看到我的适配器还有ImageLoader的。 (本类下载器只是一个包装类UniversalImageDownloader)

 公共类下载{

    / **
     *初始化与一个特定的配置的imagedownloader
     *我叫APP启动后,这个方法RIGHT
     * @参数Ç
     * /
    公共静态无效初始化(上下文C){

         //创建全局配置和初始化ImageLoader的具有这种构造

        ImageLoaderConfiguration配置=新ImageLoaderConfiguration.Builder(C)
        .threadPoolSize(20)
        .threadPriority(Thread.NORM_PRIORITY)//默认
        .tasksProcessingOrder(QueueProcessingType.FIFO)//默认
        .memoryCacheSize(20 * 1024 * 1024)
        .memoryCacheSizePercentage(15)//默认
        .discCacheSize(20 * 1024 * 1024)
        .discCacheFileCount(100)
        .discCacheFileNameGenerator(新的Hash codeFileNameGenerator())//默认
        .imageDe codeR(新BaseImageDe codeR())//默认
        。建立();

        。ImageLoader.getInstance()的init(配置);
    }

    / **
     *获取显示图像时,需要显示选项
     * @返回
     * /
    公共静态DisplayImageOptions getDisplayOptions(){

         DisplayImageOptions选项=新DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.error)
        .showImageOnFail(R.drawable.error)
        .resetViewBeforeLoading(假)//默认
        .cacheInMemory(真)//默认
        .cacheOnDisc(真)//默认
        。建立();

        返回选项;
    }

    公共静态ImageLoader的的getInstance(){
        返回ImageLoader.getInstance();
    }
}
 

和适配器:

 公共类EventListAdapter扩展ArrayAdapter<事件> {

    私人列表<事件> mList;
    私人DisplayImageOptions选项;

    公共EventListAdapter(上下文的背景下,INT list_item_resource,列表和LT;事件>对象){
        超(背景下,list_item_resource,对象);
        mList =物体;

        选项​​= Downloader.getDisplayOptions();
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){

        事件事件= mList.get(位置);

        //一个ViewHolder不断提及孩子的意见,以避免各行不必要调用findViewById()。
        ViewHolder支架=无效;

        如果(convertView == NULL){

            LayoutInflater充气=(LayoutInflater)的getContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
            convertView = inflater.inflate(R.layout.normalevent_list_item,父母,假);

            持有人=新ViewHolder();;

            holder.eventimage =(ImageView的)convertView.findViewById(R.id.ivEventImage);

            convertView.setTag(保持器);

        } 其他 {
            支架=(ViewHolder)convertView.getTag();

        }

        如果(事件!= NULL){

            holder.eventimage.setImageResource(R.drawable.loading);
            //加载图像,德code它为位图和ImageView的显示位图
            。Downloader.getInstance()displayImage(event.getImageOneURL(),holder.eventimage,期权);
        }

        返回convertView;
    }

    私有静态类ViewHolder {

        ImageView的eventimage;
    }
}
 

解决方案

我和图像下载同样的问题。我在我的DisplayImageOptions设置delayBeforeLoading(1000)解决了这个问题。它需要开始下载时用户停止一扔。

所以尽量用这个代替你的getDisplayOptions方法

 公共静态DisplayImageOptions getDisplayOptions(){

     DisplayImageOptions选项=新DisplayImageOptions.Builder()
    .showImageForEmptyUri(R.drawable.error)
    .showImageOnFail(R.drawable.error)
    .delayBeforeLoading(1000)
    .resetViewBeforeLoading(假)//默认
    .cacheInMemory(真)//默认
    .cacheOnDisc(真)//默认
    。建立();

    返回选项;
}
 

文档也建议使用此code,以避免网格/列表视图滚动滞后

 布尔pauseOnScroll = FALSE; //或真
布尔pauseOnFling = TRUE; //还是假的
PauseOnScrollListener监听器=新PauseOnScrollListener(ImageLoader的,pauseOnScroll,pauseOnFling);
listView.setOnScrollListener(听众);
 

您可以阅读这里(有用的信息的最后一个项目名单)

所以,你可以使用这种方法之一

I am using a ListView containing images. These images are loaded from the Internet inside the adapter. Therefore I am using the UniversalImageDownloader.

Unfortunately the scrolling of the ListView "lags" for a short time as soon as I scroll down where new content has to be downloaded.

I acutally expected behaviour like the ListView scrolls perfectly smooth, but the loading of the Image can of course take some more time - which should not effect the smoothness of scrolling.

Furthermore, as I scroll back up the lags occur as well. It seems as if the images are not cached properly.

Maybe my ImageLoader options are setup wrong?

Am I doing something wrong in my adapter?

The ListView contains about 20-30 Images with sizes 640x320 (around 150kb)

Below you can see my Adapter as well as the ImageLoader. (The class Downloader is just a wrapper class for the UniversalImageDownloader)

public class Downloader {

    /**
     * initializes the imagedownloader with a specific configuration
     * I CALL THIS METHOD RIGHT AFTER APP STARTUP
     * @param c
     */
    public static void initialize(Context c) {

         // Create global configuration and initialize ImageLoader with this configuration

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(c)
        .threadPoolSize(20) 
        .threadPriority(Thread.NORM_PRIORITY) // default
        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
        .memoryCacheSize(20 * 1024 * 1024)
        .memoryCacheSizePercentage(15) // default
        .discCacheSize(20 * 1024 * 1024)
        .discCacheFileCount(100)
        .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
        .imageDecoder(new BaseImageDecoder()) // default
        .build();

        ImageLoader.getInstance().init(config);
    }

    /**
     * gets the display options that are needed when displaying an image
     * @return
     */
    public static DisplayImageOptions getDisplayOptions() {

         DisplayImageOptions options = new DisplayImageOptions.Builder()
        .showImageForEmptyUri(R.drawable.error)
        .showImageOnFail(R.drawable.error)
        .resetViewBeforeLoading(false)  // default
        .cacheInMemory(true) // default
        .cacheOnDisc(true) // default
        .build();

        return options;
    }

    public static ImageLoader getInstance() {
        return ImageLoader.getInstance();
    }
}

And the adapter:

public class EventListAdapter extends ArrayAdapter<Event> {

    private List<Event> mList;
    private DisplayImageOptions options;

    public EventListAdapter(Context context, int list_item_resource, List<Event> objects) {
        super(context, list_item_resource, objects);
        mList = objects;

        options = Downloader.getDisplayOptions();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Event event = mList.get(position);

        // A ViewHolder keeps references to children views to avoid unneccessary calls to findViewById() on each row.
        ViewHolder holder = null;

        if (convertView == null) {

            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.normalevent_list_item, parent, false);

            holder = new ViewHolder();;

            holder.eventimage = (ImageView) convertView.findViewById(R.id.ivEventImage);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        if (event != null) {

            holder.eventimage.setImageResource(R.drawable.loading);
            // Load image, decode it to Bitmap and display Bitmap in ImageView
            Downloader.getInstance().displayImage(event.getImageOneURL(), holder.eventimage, options);
        }

        return convertView;
    }

    private static class ViewHolder {   

        ImageView eventimage;
    } 
}

解决方案

I had the same issue with image downloading. I solved it by setting delayBeforeLoading(1000) in my DisplayImageOptions. It is needed to start downloading when user stop fling.

so try to replace your getDisplayOptions method with this

 public static DisplayImageOptions getDisplayOptions() {

     DisplayImageOptions options = new DisplayImageOptions.Builder()
    .showImageForEmptyUri(R.drawable.error)
    .showImageOnFail(R.drawable.error)
    .delayBeforeLoading(1000) 
    .resetViewBeforeLoading(false)  // default
    .cacheInMemory(true) // default
    .cacheOnDisc(true) // default
    .build();

    return options;
}

Documentation also recommends to use this code to avoid grid/list view scroll lags

boolean pauseOnScroll = false; // or true
boolean pauseOnFling = true; // or false
PauseOnScrollListener listener = new PauseOnScrollListener(imageLoader, pauseOnScroll, pauseOnFling);
listView.setOnScrollListener(listener);

You can read it here (the last item of "Useful Info" list)

So you can use one of this methods

这篇关于使用UniversalImageDownloader不顺畅的ListView滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆