【发布时间】:2017-04-23 19:59:43
【问题描述】:
我在 for 循环中创建了毕加索的目标。我第一次收到 1 张图像,其他图像进入“onPrepareLoad”语句。我第二次收到所有图像。为什么第一次不工作?
代码:
final Set<Target> protectedFromGarbageCollectorTargets = new HashSet<>();
for (int i = 0; i < alleFotosArray.size(); i++) {
final Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
protectedFromGarbageCollectorTargets.remove(this);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();
sizeFotos.setWidth(width);
sizeFotos.setHeight(height);
sizeFotosArray.add(sizeFotos);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
protectedFromGarbageCollectorTargets.remove(this);
deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();
sizeFotos.setWidth(200);
sizeFotos.setHeight(200);
sizeFotosArray.add(sizeFotos);
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
deelnemerSizesFotos sizeFotos = new deelnemerSizesFotos();
sizeFotos.setWidth(200);
sizeFotos.setHeight(200);
sizeFotosArray.add(sizeFotos);
}
};
protectedFromGarbageCollectorTargets.add(target);
Picasso.with(c).load(alleFotosArray.get(i).getImageurl()).into(target);
}
【问题讨论】:
-
我有最终的目标,但是如何使用 ImageView 做到这一点?我没有 ImageView。
-
final不会阻止垃圾回收。您需要将Target的引用保留到不会被垃圾收集的对象。只是一个想法,您可以通过调用targetList.add(target);将成员变量List<Target> targetList = new ArrayList<>();设置为Activity以保持目标的引用。 -
我需要把'targetList.add(target)'放在哪里?
-
在你的 for 循环中。让我知道这是否能解决问题。
标签: android image bitmap imageview picasso