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

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

问题描述

使用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.

推荐答案

确保您使用的是最新版本

implementation '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<Bitmap>(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) {
            }
        });

旧答案:

使用compile '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);
            }
        });

对于compile '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

Now you might see a warning 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.

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

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

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

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