目前,我正在调查使用Picasso库替换项目中大量自定义图片加载AsyncTask的可能性,似乎很有前途。然而,有一个问题我不确定如何使用Picasso解决。
在这种情况下,我们正在下载音乐曲目的专辑封面,但当我们要显示ListView时,我们只有曲目ID。因此,我们首先必须根据曲目ID查找专辑封面URL,然后将其加载到ImageView中。目前,我们有一个AsyncTask,在doInBackground()中首先查找图像URL,然后从中加载Bitmap,最后将其传递给onPostExecute。
是否有任何方法可以使用Picasso进行预查找,还是我们必须在AsyncTask中包装Picasso调用,以便首先执行查找(感觉这有点违背其目的)。
更新:现在的工作方式:
private class AlbumArtTask extends AsyncTask<String, Bitmap, Bitmap> {
@Override
protected Bitmap doInBackground(String... strings) {
final String id = strings[0];
// This part is what I'm asking for, basically to be able to make an Async
// lookup of the url that will later be used by Picasso
final Uri uri = getAlbumArtUriFromTrackId(id);
// Creating the Bitmap and return it to be used in an ImageView
// This part is handled by Picasso
return bitmap = createBitmapFromUri(uri);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
// load image into ImageView
}
}
更新2:举个例子,让我更清楚地表达我的意思
Picasso.with(mContext)
.load(new UriProvider() {
// Get the actual image URI here
public Uri getImageUri() {
return getAlbumArtUriFromTrackId(id);
}
})
// And load it here
.into(mImageView);