android - 加载多个位图与 Picasso 问题
android - loading multiple bitmaps with Picasso issues
我正在尝试使用 Picasso 从 url 加载多个位图,并在我的 google 地图上绘制我加载的位图。
所有这些代码都在 google MapFragment
.
public void drawLocations(final UserPictureUrl[] userPicsArray) {
for (int j = 0; j < userPicsArray.length; j++) {
Location targetLocation = ... // getting location
// userPicsArray[j].getPicUrl() is the bitmap url
loadProfilePicture(userPicsArray[j].getPicUrl(), targetLocation);
}
}
private void loadProfilePicture(String picUrl, final Location location) {
friendLoadTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Toast.makeText(getActivity(), String.valueOf(counter), Toast.LENGTH_SHORT).show();
counter++;
int width = 40;
int height = 40; getResources().getDimension(R.dimen.profile_height);
// drawCanvas draws the bitmap to the map
drawCanvas(location, Bitmap.createScaledBitmap(bitmap, width, height, false), picType);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(getActivity()).load(picUrl).into(friendLoadTarget);
}
我的问题是 - 我有 3 个位图要加载,但是当 运行 应用程序时,加载行为是 意外 - 有时它只加载 1 个位图,有时全部 3 个,有时 2 个,有时 none.
如您所见,我放了一个 Toast
来测试是否真的调用了该方法,而且我几乎从未收到所有 3 个 Toast 消息。有时只有一条消息带有“1”,仅此而已..
如何确保所有图像都正确加载?
A Picasso
仅包含对 Target
的弱引用。当您在那里循环时,您将丢失前两个引用。只要您想加载 Target
s.
,就需要保留这些引用
我正在尝试使用 Picasso 从 url 加载多个位图,并在我的 google 地图上绘制我加载的位图。
所有这些代码都在 google MapFragment
.
public void drawLocations(final UserPictureUrl[] userPicsArray) {
for (int j = 0; j < userPicsArray.length; j++) {
Location targetLocation = ... // getting location
// userPicsArray[j].getPicUrl() is the bitmap url
loadProfilePicture(userPicsArray[j].getPicUrl(), targetLocation);
}
}
private void loadProfilePicture(String picUrl, final Location location) {
friendLoadTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Toast.makeText(getActivity(), String.valueOf(counter), Toast.LENGTH_SHORT).show();
counter++;
int width = 40;
int height = 40; getResources().getDimension(R.dimen.profile_height);
// drawCanvas draws the bitmap to the map
drawCanvas(location, Bitmap.createScaledBitmap(bitmap, width, height, false), picType);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(getActivity()).load(picUrl).into(friendLoadTarget);
}
我的问题是 - 我有 3 个位图要加载,但是当 运行 应用程序时,加载行为是 意外 - 有时它只加载 1 个位图,有时全部 3 个,有时 2 个,有时 none.
如您所见,我放了一个 Toast
来测试是否真的调用了该方法,而且我几乎从未收到所有 3 个 Toast 消息。有时只有一条消息带有“1”,仅此而已..
如何确保所有图像都正确加载?
A Picasso
仅包含对 Target
的弱引用。当您在那里循环时,您将丢失前两个引用。只要您想加载 Target
s.