据我所知,在这段代码中,它从 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))
}
}
}
}
}
你说得对,从 URL 下载图像可能需要一些时间,这可能会导致图像出现延迟。使用本地图像可以防止从 URL 下载图像时遇到的网络延迟,从而加快加载图像的速度。
但是,我想提请注意,如果本地图像非常大或具有高分辨率,加载仍然需要一些时间。
使用 Glide 的加载功能和 file URI 来使用本地图片而不是从 URL 下载。例如:
val fileUri = Uri.fromFile(File("path/to/local/image"))
Glide.with(this).load(fileUri).into(imageView)
您可以像这样更改代码。