滚动 ListView 时通用图像加载器滞后

Universal Image Loader lags when scrolling ListView

我有一个包含图像项的 listView。我正在使用 UniversalImageLoader 从 url 获取图像并将其设置在 ImageView 上。 我试图在另一个线程中编写代码,但由于 imageView 是在主线程中构建的,所以我的应用程序崩溃了。 问题是当我滚动 listView 时,加载图像会减慢滚动速度并使其变得迟缓。这是我的 getView():

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

    final viewHolder holder;
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (isGrid)
            convertView = mInflater.inflate(R.layout.main_grid_item, null);
        else
            convertView = mInflater.inflate(R.layout.main_list_item, null);
        holder = new viewHolder();

        holder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);

        RelativeLayout.LayoutParams ivLPI;
        if (isGrid) {
            ivLPI = new RelativeLayout.LayoutParams(imageSize, imageSize);
            ivLPI.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
            ivLPI.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
            ivLPI.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
        } else {
            ivLPI = new RelativeLayout.LayoutParams(Utils.convertDpToPx(context, 120), Utils.convertDpToPx(context, 120));
            ivLPI.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
            ivLPI.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
        }
        holder.ivImage.setLayoutParams(ivLPI);

        convertView.setTag(holder);

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

    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .delayBeforeLoading(0)
            .resetViewBeforeLoading(true)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .imageScaleType(ImageScaleType.EXACTLY)
            .build();


    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
            .denyCacheImageMultipleSizesInMemory()
            .defaultDisplayImageOptions(defaultOptions)
            .diskCacheExtraOptions(480, 320, null)
            .threadPoolSize(10)
            .build();

    ImageLoader.getInstance().init(config);

    final ProgressBar spinner = (ProgressBar) convertView.findViewById(R.id.spinner);

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage(data.get(position).getThumbURL(), holder.ivImage, new ImageLoadingListener() {

        @Override
        public void onLoadingStarted(String s, View view) {
            spinner.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String s, View view, FailReason failReason) {
            spinner.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingComplete(String s, View view, Bitmap bitmap) {
            spinner.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingCancelled(String s, View view) {

            spinner.setVisibility(View.GONE);
        }
    });
    return convertView;
}

从你的代码中我看到你的 DisplayImageOptionsImageLoaderConfiguration 与你每次创建它的原因完全没有变化。
所以把那两个对象和 ImageLoader imageLoader = ImageLoader.getInstance(); 放在 onCreate() 中。它会解决你的问题。