如何从Glide中获取drawable?实际上,我想要从Glide返回drawable,它加载图像并将其放入imageView。

9

以下是我的代码:

d.setBounds(15, 8, d.getIntrinsicWidth(), d.getIntrinsicHeight());
ImageSpan span = new ImageSpan(d,ImageSpan.ALIGN_BOTTOM) {}

我想获取drawable,因为我将利用drawable函数的用途。这就是为什么我想从Glide中获取drawable。 - kshitij
1
请查看此链接:https://stackoverflow.com/help/how-to-ask。 - Deˣ
1个回答

19

使用最新版本的Glide

implementation 'com.github.bumptech.glide:glide:4.9.0'

Kotlin:

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

位图大小:

如果您想使用原始图像的大小,请使用上述默认构造函数,否则您可以传递所需的位图大小。

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

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