android - 使用 Facebook 的 Fresco 加载位图

标签 android facebook bitmap picasso fresco

我正在尝试用 Fresco 替换我的 Android 应用程序中的 Picasso。但是我不确定如何使用 Fresco 简单地加载位图。

对于 Picasso,我只会执行以下操作。

Bitmap poster = Picasso.with(getActivity())
                    .load(url)
                    .resize(Utils.convertDpToPixel(WIDTH,HEIGHT))
                    .centerCrop()
                    .get();

我一直无法弄清楚如何使用此 Fresco 创建位图。有任何想法吗?

最佳答案

正如 Fresco 所说:

如果您对管道的请求是解码图像 - Android 位图,您可以利用我们更易于使用的 BaseBitmapDataSubscriber:

dataSource.subscribe(new BaseBitmapDataSubscriber() {
    @Override
    public void onNewResultImpl(@Nullable Bitmap bitmap) {
       // You can use the bitmap in only limited ways
      // No need to do any cleanup.
    }

    @Override
    public void onFailureImpl(DataSource dataSource) {
      // No cleanup required here.
    }
  },
  executor);

您不能将位图分配给任何不在 onNewResultImpl 方法范围内的变量。

http://frescolib.org/docs/datasources-datasubscribers.html#_

我的代码:

public void setDataSubscriber(Context context, Uri uri, int width, int height){
    DataSubscriber dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableBitmap>>() {
        @Override
        public void onNewResultImpl(
                DataSource<CloseableReference<CloseableBitmap>> dataSource) {
            if (!dataSource.isFinished()) {
                return;
            }
            CloseableReference<CloseableBitmap> imageReference = dataSource.getResult();
            if (imageReference != null) {
                final CloseableReference<CloseableBitmap> closeableReference = imageReference.clone();
                try {
                    CloseableBitmap closeableBitmap = closeableReference.get();
                    Bitmap bitmap  = closeableBitmap.getUnderlyingBitmap();
                    if(bitmap != null && !bitmap.isRecycled()) {
                        //you can use bitmap here
                    }
                } finally {
                    imageReference.close();
                    closeableReference.close();
                }
            }
        }
        @Override
        public void onFailureImpl(DataSource dataSource) {
            Throwable throwable = dataSource.getFailureCause();
            // handle failure
        }
    };
    getBitmap(context, uri, width, height, dataSubscriber);
}

/**
 *
 * @param context
 * @param uri
 * @param width          
 * @param height         
 * @param dataSubscriber
 */
public void getBitmap(Context context, Uri uri, int width, int height, DataSubscriber dataSubscriber){
    ImagePipeline imagePipeline = Fresco.getImagePipeline();
    ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri);
    if(width > 0 && height > 0){
        builder.setResizeOptions(new ResizeOptions(width, height));
    }
    ImageRequest request = builder.build();
    DataSource<CloseableReference<CloseableImage>>
            dataSource = imagePipeline.fetchDecodedImage(request, context);
    dataSource.subscribe(dataSubscriber, UiThreadExecutorService.getInstance());
}

关于android - 使用 Facebook 的 Fresco 加载位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29476677/

相关文章:

java - 应用程序崩溃后,Android studio 中的清理项目和几个选项被禁用

node.js - NodeJS Passport - Facebook 身份验证

database - Facebook 更快还是本地数据库?

.net - 如何使用 PDFsharp .NET 库将 PDF 页面导出为图像?

android - 罗盘方向因手机方向而异

android - Google MapView 上的动画标记

android - DialogFragment 中的动画 View

Android Facebook ..如何获取AccessToken

android - android中的灰度位图

Android - getDrawingCache - 黑屏