在使用Glide加载缩略图的同时获取位图

4
我有一个ImageView,需要加载一张大图。我有一张低分辨率的该图片可用作预览,在高清晰度图片下载完成并出现在屏幕上之前,我希望能够加载该预览图。
使用以下方法:
private void loadImageThumbnailRequest() {
    // setup Glide request without the into() method
    DrawableRequestBuilder<String> thumbnailRequest = Glide
            .with( context )
            .load( eatFoodyImages[2] );

    // pass the request as a a parameter to the thumbnail request
    Glide
            .with( context )
            .load( UsageExampleGifAndVideos.gifUrl )
            .thumbnail( thumbnailRequest )
            .into( imageView3 );
}

我可以得到结果,但无法获得下载图像的位图,因为我不能在同一Glide实例中同时使用.asBitmap、.thumbnail和SimpleTarget。
我的实际代码:
.asBitmap()
            .into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                @Override
                public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
                    mWallpaperImageView.setImageBitmap(resource);
                    createPaletteAsync(resource);
                }
            });
2个回答

0

如果你正在使用Glide 4,只需创建一个监听器在最后获取它:

.listener(new RequestListener<Bitmap>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                        //do what you want with resource;
                        return false;
                    }
                })


0

.into 不能用于预加载 Bitmap。但是,.preload 可以。如果不加上 preload(),那么 @Amit 的答案就不太准确了。请看我的例子:

        Glide.with(context)
                .asBitmap()
                .load(imageUrl)
                .thumbnail(0.3f)
                .listener(object: RequestListener<Bitmap> {
                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Bitmap>?, isFirstResource: Boolean): Boolean {
                        System.out.println("here's your exception")
                        return true
                    }

                    override fun onResourceReady(resource: Bitmap?, model: Any?, target: Target<Bitmap>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                        System.out.println("here's your bitmap")
                        return true
                    }

                })
                .preload()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接