ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 使用Facebook的Fresco加载位图

2019-09-27 09:25:08  阅读:284  来源: 互联网

标签:fresco android facebook picasso bitmap


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

毕加索我会做以下几点.

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

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

解决方法:

正如弗雷斯科所说:

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

标签:fresco,android,facebook,picasso,bitmap
来源: https://codeday.me/bug/20190927/1823293.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有