在背景上加载毕加索的多个图像

时间:2015-03-25 15:22:01

标签: android caching imageview lazy-loading picasso

我正在尝试在Picasso的背景下加载一个包含20个网址的数组。到目前为止,我有下一个代码:

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 个答案:

答案 0 :(得分:5)

我不得不使用:  Picasso.with(getActivity().getApplicationContext()).load(p.getImage()).fetch();

以下是参考:https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html

答案 1 :(得分: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); 

}

您可以参考示例here