【发布时间】:2017-04-13 09:03:41
【问题描述】:
早些时候,我认为 Fresco 可以完全替代毕加索。
例如,我可以使用 Picasso 加载位图,并使用this SO answer中建议的解决方案将其设置在 any 视图上。
Fresco 是否支持此功能?
更具体地说,我的问题是否可以在任何视图上使用 Fresco 设置加载的图像,无需创建自定义视图?
【问题讨论】:
早些时候,我认为 Fresco 可以完全替代毕加索。
例如,我可以使用 Picasso 加载位图,并使用this SO answer中建议的解决方案将其设置在 any 视图上。
Fresco 是否支持此功能?
更具体地说,我的问题是否可以在任何视图上使用 Fresco 设置加载的图像,无需创建自定义视图?
【问题讨论】:
当然可以。使用下一个代码:
Uri imageUri = com.facebook.common.util.UriUtil.getUriForResourceId(imgResId);
// or if you use link:
// Uri imageUri = Uri.parse(webLinkToTheImage);
ImageRequestBuilder builder = ImageRequestBuilder
.newBuilderWithSource(imageUri)
.setRequestPriority(Priority.HIGH)
.setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH);
final DataSource<CloseableReference<CloseableImage>> dataSource =
Fresco.getImagePipeline().fetchDecodedImage(builder.build(), imageUri);
try {
dataSource.subscribe(new BaseBitmapDataSubscriber() {
@Override
public void onNewResultImpl(@Nullable Bitmap bitmap) {
if (null != bitmap) {
//TODO use bitmap
}
}
@Override
public void onFailureImpl(DataSource dataSource) {
if (dataSource != null) {
dataSource.close();
}
}
}, new MainThreadExecutor());
} finally {
if (dataSource != null) {
dataSource.close();
}
}
public class MainThreadExecutor implements Executor {
private final Handler handler = new Handler(Looper.getMainLooper());
@Override
public void execute(@NonNull Runnable r) {
handler.post(r);
}
}
【讨论】: