首页 > 解决方案 > 使用 Glide 从 filesDir() 加载九个补丁

问题描述

我正在尝试从我之前从服务器下载的filesDir()加载.9.png到View

Glide.with(context).asDrawable().load(f.absolutePath).dontTransform().into(object : CustomTarget<Drawable>() {
            override fun onLoadStarted(placeholder: Drawable?) {
                super.onLoadStarted(placeholder)
                Log.v("DRAWABLE_TEST", "onLoadStarted")
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                super.onLoadFailed(errorDrawable)
                Log.e("DRAWABLE_TEST", "onLoadFailed")
            }

            override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
                Log.v("DRAWABLE_TEST", "onResourceReady")
                view.background = resource
            }

            override fun onLoadCleared(placeholder: Drawable?) {
            }
        })

但我只得到拉伸的正常可绘制对象。

知道我应该如何加载它吗?

标签: androidkotlinandroid-glidenine-patch

解决方案


好的,我已经做到了,但 Glide 没有:

val bmp = BitmapFactory.decodeFile(f.absolutePath)
val chunk = bmp.ninePatchChunk
val ninePatchDrawable = NinePatchDrawable(context.resources, bmp, chunk, Rect(), null)

但是,在下载 *.9.png 图像之前,它们应该在一些 APK 中构建,然后上传到服务器。来源在这里


推荐阅读