如何通过 glide 使用本地图像而不是图像的 URL?

问题描述 投票:0回答:2

据我所知,在这段代码中,它从 url 下载图像,这需要时间,所以它会延迟显示。我认为如果我使用本地图像,这种延迟就会消失。如何使用本地图像而不是下载的图像?

private fun updateView() {
        imageViewBackground.post {
            ThreadUtil.startThread {
                val imageUrl = "https://firebasestorage.googleapis.com/v0/b/tff-sample.appspot.com/o/TeacherPhoto%2FteacherPhoto.jpg?alt=media&token=7d462beb-8c6d-4467-9139-1de0f75b8563"
                val futureTarget: FutureTarget<Bitmap> = Glide.with(this)
                .asBitmap()
                .load(imageUrl)
                .submit (imageViewBackground.width, imageViewBackground.height)
                val bitmap = futureTarget.get()

                ThreadUtil.startUIThread(delayMillis = 0) {
                    imageViewBackground.setImageBitmap(bitmap)

                    frameLayoutInputArea.post {

                        var cropBitmap = Bitmap.createBitmap(imageViewBackground.width, imageViewBackground.height, Bitmap.Config.ARGB_8888)
                        val canvas = Canvas(cropBitmap)
                        imageViewBackground.draw(canvas)

                        cropBitmap = Bitmap.createBitmap(cropBitmap, frameLayoutInputArea.x.toInt(), frameLayoutInputArea.y.toInt(), frameLayoutInputArea.width, frameLayoutInputArea.height)

                        imageViewBlur.setImageBitmap(BitmapUtil.blurBitmap(this, cropBitmap))

                    }
                }

            }
        }
    }
android kotlin android-glide
2个回答
1
投票

Glide 有几个

load
功能,包括采用 a
File
a
Uri
an
Integer
资源 ID
的功能。因此,无论您如何存储本地图像,都有一个功能可以将其插入


0
投票

你说得对,从 URL 下载图像可能需要一些时间,这可能会导致图像出现延迟。使用本地图像可以防止从 URL 下载图像时遇到的网络延迟,从而加快加载图像的速度。

但是,我想提请注意,如果本地图像非常大或具有高分辨率,加载仍然需要一些时间。

使用 Glide 的加载功能和 file URI 来使用本地图片而不是从 URL 下载。例如:

val fileUri = Uri.fromFile(File("path/to/local/image"))
Glide.with(this).load(fileUri).into(imageView)

您可以像这样更改代码。

© www.soinside.com 2019 - 2024. All rights reserved.