【发布时间】:2015-05-13 09:46:13
【问题描述】:
所以我目前正在研究用毕加索库替换项目中大量自定义图像加载AsyncTasks 的可能性,这似乎很有希望。但是,我不完全确定如何使用 Picasso 解决一个问题。
在这种情况下,我们正在下载音乐曲目的专辑封面,但是当我们的 ListView 要显示时,我们只有曲目 ID。因此,首先我们必须根据曲目 ID 查找专辑封面 URL,然后将其加载到 ImageView 中。目前我们有一个AsyncTask,我们在doInBackground()中首先查找图像URL,然后从中加载Bitmap,然后将其粘贴到onPostExecute。
有什么方法可以使用 Picasso 进行这种预查找,或者我们是否必须将 Picasso 调用包装在首先执行查找的 AsyncTask 中(感觉有点违背目的)。
更新:现在的工作方式:
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);
【问题讨论】:
-
加载前查找的目的是什么?
-
是否因为您需要位图用于其他目的?如果是这种情况,您可以在之后直接从 imageview 中获取它。
-
预查找是什么意思?您必须显示来自本地存储的占位符,或者必须显示来自服务器的原始图像以外的预查找图像..
-
目前正在编辑问题,但简而言之,我们正在为曲目加载专辑封面,当我们的 ListView 要显示时,我们只有曲目 ID。因此,我们首先需要根据曲目 id 查找专辑封面 url,然后将该 url 加载到 ImageView 中。
-
如果没有找到图片,你想在查找后显示默认图片吗?