使用Picasso在后台加载多张图片

8

我正在尝试使用Picasso在后台加载包含20个URL的数组。到目前为止,我有以下代码:

Log.d("GAME", "Loading all images");
for (int i = gamePieces.length-1; i >= 0; i--) {
   GamePiece p = gamePieces[i];
   Log.d("GAME", "I will load " + p.getImage());
   Picasso.with(context).load(p.getImage()).into(target);
}
//loading the first one
Picasso.with(context).load(piece.getImage()).into(target);

我要翻译的内容是:target目标对象是下一个:

Target target = new Target() {
       @Override
       public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
           Log.d("GAME", "Image loaded" + ++test);
           gameImage.setImageBitmap(bitmap); //ImageView to show the images
       }

       @Override
       public void onBitmapFailed(Drawable arg0) {}

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

我想预加载图片,以便在用户点击按钮时可以逐个显示在ImageView中。第一张图片加载得非常快(很酷),但是for循环中的其他图片从未被加载。我该怎么解决?我需要在for循环中开始加载这些图像。

2
1.) 始终在某个地方保持对Picasso Targets的强引用,因为Picasso本身只会保留一个WeakReference - 并且在图像加载完成之前,Target将被垃圾回收。2.) 我不是完全确定,但从源代码来看,似乎Picasso只会处理传递到“into()”中的每个Target或ImageView实例的一个请求 - 也许你可以自己查看以确认或否认。 - david.mihola
@david.mihola 我该如何保持对Picasso目标的强引用?你所说的“也许自己看看以确认或否认”是什么意思?那是一种方法吗? - Fernando Santiago
2个回答

5

1
参考链接已失效。 - Biswajit Panday

0

也许你可以尝试以下操作:

Picasso mPicasso = Picasso.with(context); //Single instance

//if you are indeed loading the first one this should be in top, before the iteration.
Picasso.with(context).load(piece.getImage()).into(target);

Log.d("GAME", "Loading all images");
for (int i = gamePieces.length-1; i >= 0; i--) {

   GamePiece p = gamePieces[i];
   Log.d("GAME", "I will load " + p.getImage());
   mPicasso.load(p.getImage()).into(target); 

}

您可以随时参考这里的示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接