4

我正在使用包含图像的 ListView。这些图像是从适配器内部的 Internet 加载的。因此我使用的是UniversalImageDownloader

不幸的是,只要我向下滚动必须下载新内容的位置,ListView 的滚动就会“滞后”一小段时间。

我真的期望像 ListView 这样的行为滚动非常平滑,但是加载图像当然需要更多时间——这不应该影响滚动的平滑度。

此外,当我向上滚动时,也会出现滞后。似乎图像没有正确缓存。

也许我的 ImageLoader 选项设置错误?

我的适配器做错了吗?

ListView 包含大约 20-30 个图像,大小为 640x320(大约 150kb)

下面你可以看到我的适配器以及 ImageLoader。(Downloader 类只是 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();
    }
}

和适配器:

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;
    } 
}
4

2 回答 2

8

我在下载图片时遇到了同样的问题。我通过在 DisplayImageOptions 中设置 delayBeforeLoading(1000) 解决了这个问题。当用户停止投掷时需要开始下载。

所以尝试用这个替换你的 getDisplayOptions 方法

 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;
}

文档还建议使用此代码来避免网格/列表视图滚动滞后

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

你可以在这里阅读(“有用信息”列表的最后一项)

所以你可以使用其中一种方法

于 2013-08-14T12:22:54.463 回答
1

@Sergey Pekar:你救了我的命!:) 对于少量图像,我总是将 UIL 用于 ListView,但是随着数量的增加,我总是在图像的第一次加载(滚动时)时出现延迟。所以我也尝试了延迟,它对其他 UIL 配置和选项有所帮助,但我并不高兴。然后我尝试了不同的图像加载库(毕加索、AQuery、Volley、UrlImageViewHelper、自定义代码)。它们都比 UIL 工作得更快(在第一次加载时),但它们对我来说没有足够的选择。因此,我在过去 2 天寻找有关此滞后问题的解决方案,感谢上帝,我终于在这里找到了您的帖子!解决方案是 PauseOnScrollListener!

还有什么(除了 PauseOnScrollListener 之外)通过扩展 ImageView 以停止 requestLayout 来稍微提高滚动平滑度,如下所示:

public class CustomImageView extends ImageView
{
    public CustomImageView (Context context, AttributeSet attributeset, int int_style)
    {
        super(context, attributeset, int_style);
    }

    public CustomImageView (Context context, AttributeSet attributeset)
    {
        super(context, attributeset);
    }

    public CustomImageView (Context context)
    {
        super(context);
    }

    @Override
    public void requestLayout() 
    {
        // Do nothing here
    }
}

欲了解更多信息,请参阅这篇文章:
ListView very slow when scrolling (using ViewHolder/recycling)

于 2014-03-29T21:09:37.647 回答