在背景上使用 Picasso 加载多个图像
Loading multiple images with Picasso on background
我正在尝试使用 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 循环中开始加载。
我不得不使用:
Picasso.with(getActivity().getApplicationContext()).load(p.getImage()).fetch();
引用如下:https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html
也许您可以尝试执行以下操作:
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
我正在尝试使用 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 循环中开始加载。
我不得不使用:
Picasso.with(getActivity().getApplicationContext()).load(p.getImage()).fetch();
引用如下:https://square.github.io/picasso/2.x/picasso/com/squareup/picasso/RequestCreator.html
也许您可以尝试执行以下操作:
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