如何使用 glide 将图像下载到位图? [英] How does one use glide to download an image into a bitmap?

查看:29
本文介绍了如何使用 glide 将图像下载到位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Glide 将 URL 下载到 ImageView 非常容易:

Downloading a URL into an ImageView is very easy using Glide:

Glide
   .with(context)
   .load(getIntent().getData())
   .placeholder(R.drawable.ic_loading)
   .centerCrop()
   .into(imageView);

我想知道我是否也可以下载到 Bitmap 中?我想下载到一个原始位图,然后我可以使用其他工具进行操作.我已经完成了代码,但不知道该怎么做.

I'm wondering if I can download into a Bitmap as well? I'd like to download into a raw bitmap that I can then manipulate using other tools. I've been through the code and don't see how to do it.

推荐答案

确保您使用的是最新版本

实现'com.github.bumptech.glide:glide:4.10.0'

科特林:

Glide.with(this)
        .asBitmap()
        .load(imagePath)
        .into(object : CustomTarget<Bitmap>(){
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                imageView.setImageBitmap(resource)
            }
            override fun onLoadCleared(placeholder: Drawable?) {
                // this is called when imageView is cleared on lifecycle call or for
                // some other reason.
                // if you are referencing the bitmap somewhere else too other than this imageView
                // clear it here as you can no longer have the bitmap
            }
        })

位图大小:

如果你想使用图像的原始大小,使用上面的默认构造函数,否则你可以传递你想要的位图大小

if you want to use the original size of the image use the default constructor as above, else You can pass your desired size for bitmap

into(object : CustomTarget(1980, 1080)

Java:

Glide.with(this)
        .asBitmap()
        .load(path)
        .into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {
            }
        });

旧答案:

使用编译'com.github.bumptech.glide:glide:4.8.0'及以下

Glide.with(this)
        .asBitmap()
        .load(path)
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);
            }
        });

对于编译'com.github.bumptech.glide:glide:3.7.0'及以下

Glide.with(this)
        .load(path)
        .asBitmap()
        .into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                imageView.setImageBitmap(resource);
            }
        });

现在您可能会看到警告SimpleTarget is deprecated

原因:

弃用 SimpleTarget 的主要目的是警告您它诱使您破坏 Glide 的 API 合同的方式.具体来说,它不会强迫您停止使用任何清除 SimpleTarget 后加载的资源,这可以导致崩溃和图形损坏.

The main point of deprecating SimpleTarget is to warn you about the ways in which it tempts you to break Glide's API contract. Specifically, it doesn't do anything to force you to stop using any resource you've loaded once the SimpleTarget is cleared, which can lead to crashes and graphical corruption.

SimpleTarget 仍然可以使用,只要您确保在清除 imageView 后不使用位图.

The SimpleTarget still can be used as long you make sure you are not using the bitmap once the imageView is cleared.

这篇关于如何使用 glide 将图像下载到位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆