代码之家  ›  专栏  ›  技术社区  ›  Fernando Santiago

加载背景为毕加索的多幅图像

  •  7
  • Fernando Santiago  · 技术社区  · 11 年前

    我试图在毕加索的背景中加载一个由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 回复  |  直到 11 年前
        1
  •  5
  •   PrashanD    8 年前

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

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

        2
  •  0
  •   Joel    11 年前

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

    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