You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Glide加载本地图片替代网络URL图片?

用本地图片替换网络图片消除加载延迟的修改方案

场景1:图片存放在res/drawable目录下

将Glide的网络URL替换为Android资源ID即可直接加载本地资源,修改后的代码如下:

private fun updateView() {
    imageViewBackground.post {
        ThreadUtil.startThread {
            // 替换为你放在drawable目录下的图片资源ID
            val localImageRes = R.drawable.teacher_photo
            val futureTarget: FutureTarget<Bitmap> = Glide.with(this)
                .asBitmap()
                .load(localImageRes) // 改为加载本地资源
                .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))
                }
            }
        }
    }
}

场景2:图片是手机存储中的本地文件

如果图片在内部/外部存储路径下,直接加载对应的File对象或路径字符串:

private fun updateView() {
    imageViewBackground.post {
        ThreadUtil.startThread {
            // 替换为本地图片的绝对路径
            val localImagePath = "/storage/emulated/0/DCIM/Camera/teacherPhoto.jpg"
            val futureTarget: FutureTarget<Bitmap> = Glide.with(this)
                .asBitmap()
                .load(File(localImagePath)) // 改为加载本地文件
                .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))
                }
            }
        }
    }
}

优化建议:简化线程管理

Glide本身已经封装了异步加载逻辑,无需手动创建线程,可简化代码:

private fun updateView() {
    imageViewBackground.post {
        Glide.with(this)
            .asBitmap()
            .load(R.drawable.teacher_photo) // 替换为对应资源ID或File对象
            .into(object : CustomTarget<Bitmap>(imageViewBackground.width, imageViewBackground.height) {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    imageViewBackground.setImageBitmap(resource)
                    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@YourActivity, cropBitmap))
                    }
                }

                override fun onLoadCleared(placeholder: Drawable?) {
                    // 可选:资源回收时的处理逻辑
                }
            })
    }
}

注意:替换this@YourActivity为当前Activity的类名,比如this@MainActivity。

内容的提问来源于stack exchange,提问作者Vugar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 00:00:36