首次加载时未调用Target对象的onBitmapLoaded [英] onBitmapLoaded of Target object not called on first load

查看:96
本文介绍了首次加载时未调用Target对象的onBitmapLoaded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的职能中:

public void getPointMarkerFromUrl(final String url, final OnBitmapDescriptorRetrievedListener listener) {
final int maxSize = context.getResources().getDimensionPixelSize(R.dimen.icon_max_size);
Target t = new Target() {
  @Override
  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    if (bitmap != null)
      listener.bitmapRetrieved(getBitmapDescriptorInCache(url, bitmap));
    else
      loadDefaultMarker(listener);
  }

  @Override
  public void onBitmapFailed(Drawable errorDrawable) {
    loadDefaultMarker(listener);
  }

  @Override
  public void onPrepareLoad(Drawable placeHolderDrawable) {
  }
};

Picasso.with(context)
    .load(url)
    .resize(maxSize, maxSize)
    .into(t);
}

onBitmapLoaded()从未在我第一次加载图片时调用.我已经读过类似 https://github.com/square/picasso/issues/39 这样的主题,使用fetch(Target t)方法(这似乎是引用弱的问题...),但是此功能在上一版的毕加索(2.3.2)中不可用.我只有一个fetch()方法,但不能同时使用into(mytarget)

The onBitmapLoaded() is never called the first time I load pictures. I've read some topic like https://github.com/square/picasso/issues/39 which recommand to use fetch(Target t) method (it seems to be a problem of weak reference...), but this function is not available in the last release of picasso (2.3.2). I've only a fetch() method, but I cannot use into(mytarget) at the same time

能否请您解释一下如何将fetch()与自定义目标一起使用?谢谢.

Could you explain me how to use fetch() with a custom Target please ? Thank you.

Doc: http://square.github.io/picasso /javadoc/com/squareup/picasso/RequestCreator.html#fetch--

推荐答案

正如其他受访者(@lukas和@mradzinski)所指出的那样,毕加索只保留了对Target对象的弱引用.虽然您可以在一个类中存储强引用Target,但是如果Target以任何方式引用View仍然会出现问题,因为您还将有效地保持对该View的强引用也是如此(这是毕加索明确帮助您避免的事情之一.)

As noted by the other respondents (@lukas and @mradzinski), Picasso only keeps a weak reference to the Target object. While you can store a strong reference Target in one of your classes, this can still be problematic if the Target references a View in any manner, since you'll effectively also be keeping a strong reference to that View as well (which is one of the things that Picasso explicitly helps you avoid).

如果您处于这种情况,建议您将Target标记为View:

If you are in this situation, I'd recommend tagging the Target to the View:

final ImageView imageView = ... // The view Picasso is loading an image into
final Target target = new Target{...};
imageView.setTag(target);

这种方法的好处是让毕加索可以为您处理所有事情.它会为每个视图管理WeakReference对象-不再需要一个视图时,无论处理该图像的Target都将被释放,因此您不会因为目标寿命长而陷入内存泄漏,但只要目标处于活动状态,您的目标就会持续存在.

This approach has the benefit of letting Picasso handle everything for you. It will manage the WeakReference objects for each of your views - as soon as one is no longer needed, whatever Target processing the image will also be released, so you're not stuck with memory leaks due to long-lived targets, but your Target will last as long as its view is alive.

这篇关于首次加载时未调用Target对象的onBitmapLoaded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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