【问题标题】:Glide: Non ImageView target compression/resizing by default?Glide:默认情况下非 ImageView 目标压缩/调整大小?
【发布时间】:2019-05-23 17:42:24
【问题描述】:

我正在加载 Google Map GroundOverlayGlide 像这样

Glide.with(this)
            .asBitmap()
            .load("url")
            .into(updateOverlayTarget2)

目标是

private val updateOverlayTarget = object : SimpleTarget<Bitmap>() {
        override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
            val bounds = LatLngBounds(LatLng(34.5362, -96.9535), LatLng(39.9342, -89.8475))
            val overlay = GroundOverlayOptions()
                .image(BitmapDescriptorFactory.fromBitmap(resource))
                .positionFromBounds(bounds)

            googleMap1?.addGroundOverlay(overlay)
        }
    }

这对我很有用。但是,当我下载远程图像时,将其放在drawable 文件夹中,而不是使用BitmapDescriptorFactory.fromBitmap(resource),我使用BitmapDescriptorFactory.fromResource(resourceId),我立即收到OutOf Memory 错误。我的图像中有 alpha 通道。

我在这里有点困惑。 Glide 不能在这里使用默认的 RGB_565,因为这种格式没有 alpha。它是否在进行其他压缩?

【问题讨论】:

    标签: android android-glide android-image


    【解决方案1】:

    Glide 不能在这里使用默认的 RGB_565,因为其中没有 alpha 这种格式。它是否在进行其他压缩?

    如果您尝试直接使用onResourceReady 中可绘制的新图像,如果图像很大,则很可能会抛出 OOM,因为它尚未加载。

    你必须从drawable异步加载图片,例如:

    Glide.with(this)
            .asBitmap()
            .load(R.drawable.your_image)
            .into(object : CustomTarget<Bitmap>(){
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    // Now here you can use resource
                }
                override fun onLoadCleared(placeholder: Drawable?) {
                }
            })
    

    或者你也可以使用它来同步获取位图

    val bitmap = Glide.with(this)
                .asBitmap()
                .load(R.drawable.your_image)
                .submit().get()
    

    【讨论】:

    • 我不明白。如果图像在内存占用上加载synchronouslyasynchronously会有什么区别?
    • .submit().get() 会同步返回图片
    • 抱歉纠正了错误。在内存使用方面,两个图像将占用相同的内存量,asynchronous 在加载时不会冻结 UI。
    • 好的,感谢您的努力,但我的问题是关于使用 Glide 加载且没有在非视图目标中的图像的内存占用
    • 仅供参考 BitmapDescriptorFactory.fromResource(resourceId) 来自地图 SDK。使用BitmapDescriptorFactory.fromBitmap(resource) 时,您使用的是预加载在内存中(通过 Glide)的图像,而使用 BitmapDescriptorFactory.fromResource(resourceId) 时,您尝试使用没有 Glide 的新图像。
    猜你喜欢
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 2013-08-31
    • 2018-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    相关资源
    最近更新 更多