使用Glide或coil来显示和缓存生成的位图

发布于 2025-01-14 04:17:25 字数 869 浏览 2 评论 0原文

我正在尝试优化我的recyclerview,我正在加载从函数生成的pdf缩略图,我设法从返回我的函数显示这些位图,但无法将它们保存在Glide缓存中。每次都会重新生成一个新的,我使用下面的代码片段实现了这个结果。目标是从缓存中加载缩略图(如果已存在),否则生成缩略图。我也听说过 Glide 的签名,但是当我添加这个参数时,我得到了一个错误...我还听说过线圈库,它似乎也提供了这种功能。 有人知道该怎么做吗?

谢谢

val bitmapPdf: Bitmap = BitmapFactory.decodeResource(context.resources,R.drawable.ic_pdf)

        Glide.with(context).asBitmap().load(bitmapPdf).into(object : CustomTarget<Bitmap>() {
            override fun onLoadCleared(placeholder: Drawable?) {
            }

            override fun onResourceReady(
                resource: Bitmap,
                transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?
            ) {
                val bitmapPdf = pdfToBitmap(currentFile)  //My bitmap generator
                holder.file_img.setImageBitmap(bitmapPdf)
            }

        })

I'm trying to optimize my recyclerview, I'm loading in pdf thumbnails generated from a function, I manage to display these bitmaps from a function which return me one’s but impossible to save them in the Glide cache. A new one is regenerated each time, I achieved this result using the piece of code bellow. The goal is to load the thumbnail from the cache if it already exists, otherwise generate it. I heard about signatures also with Glide but when I add this parameter I get an error... Also I heard about coil library which seems to offers this kind of features too.
Someone know how to do it?

Thank you

val bitmapPdf: Bitmap = BitmapFactory.decodeResource(context.resources,R.drawable.ic_pdf)

        Glide.with(context).asBitmap().load(bitmapPdf).into(object : CustomTarget<Bitmap>() {
            override fun onLoadCleared(placeholder: Drawable?) {
            }

            override fun onResourceReady(
                resource: Bitmap,
                transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?
            ) {
                val bitmapPdf = pdfToBitmap(currentFile)  //My bitmap generator
                holder.file_img.setImageBitmap(bitmapPdf)
            }

        })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文