【问题标题】:How to set LinearLayout background image with Fresco in code如何在代码中使用 Fresco 设置 LinearLayout 背景图像
【发布时间】:2017-04-13 09:03:41
【问题描述】:

早些时候,我认为 Fresco 可以完全替代毕加索。

例如,我可以使用 Picasso 加载位图,并使用this SO answer中建议的解决方案将其设置在 any 视图上。

Fresco 是否支持此功能?

更具体地说,我的问题是否可以在任何视图上使用 Fresco 设置加载的图像,无需创建自定义视图

【问题讨论】:

    标签: android fresco


    【解决方案1】:

    当然可以。使用下一个代码:

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 2022-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多