使用Facebook的Fresco加载位图

tre*_*ear 13 android facebook bitmap picasso fresco

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

毕加索我会做以下几点.

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

我一直无法弄清楚如何用这个Fresco创建一个Bitmap.有任何想法吗?

Bin*_*Sun 21

正如弗雷斯科所说:

如果您对管道的请求是针对解码图像(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);
Run Code Online (Sandbox Code Playgroud)

您不能将位图分配给不在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());
}
Run Code Online (Sandbox Code Playgroud)


小智 2

为此,您可以直接使用 Fresco 的图像管道:

http://frescolib.org/docs/using-image-pipeline.html

不过,如果你不介意我问的话——这里的动机是什么?为什么需要位图本身?

  • 我确信我会在我的新项目中尝试 Fresco,但只是指出 - 恕我直言,将下载的图像作为“位图”对象(或您可以直接操作的其他格式)访问是许多功能的重要功能开发人员可能希望看到 (3认同)