本文介绍了有时,毕加索不加载从内存中缓存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 private class CustomAdapter extends CursorAdapter {

@Override
public void bindView(View view, Context context, Cursor cursor) {
    if (view != null) {

        String url = cursor.getString(CONTENT_URL_COLUMN);
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        final ImageView imageView = viewHolder.mImageViewIcon;
        final TextView textView = viewHolder.mTextViewName;

            Picasso.with(context).load(url).into(new Target() {

                @Override
                public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
                    imageView.setImageBitmap(arg0);
                    imageView.setVisibility(View.VISIBLE);
                    textView.setVisibility(View.GONE);
                }

                @Override
                public void onBitmapFailed(Drawable arg0) {
                    imageView.setVisibility(View.GONE);
                    textView.setVisibility(View.VISIBLE);
                }
             });
        }
    }
}
}

如果得到已经下载的图像列表,然后在快速度的滚动列表, onBitmapLoaded()的方法调用,并从内存中缓存加载图像。但有时 onBitmapFailed()调用。为什么呢?

If list of images get already downloaded, then on fastly scrolling the list,onBitmapLoaded() method called and load the image from memory cache. But sometimes onBitmapFailed() called. Why ?

推荐答案

目标是收集,因为没有持有对它的引用被当作垃圾。毕加索采用了的WeakReference 控股的ImageView s或当目标取值

Your Target is being garbage collected because nothing is holding a reference to it. Picasso uses a WeakReference when holding ImageViews or Targets.

不过,你不需要使用目标可言。只需简单的使用回调。走进并通过的ImageView 直接

However, you need not use Target at all. Simply use the callback of .into and pass the ImageView directly.

Picasso.with(context).load(url).into(imageView, new Callback() {
  @Override public void onSuccess() {
    imageView.setVisibility(VISIBLE);
    textView.setVisibility(GONE);
  }

  @Override public void onError() {
    imageView.setVisibility(GONE);
    textView.setVisibility(VISIBLE);
  }
});

这篇关于有时,毕加索不加载从内存中缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:32