首页 > 解决方案 > Glide:默认情况下非 ImageView 目标压缩/调整大小?

问题描述

我正在加载Google Map GroundOverlay这样Glide

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。它是否在进行其他压缩?

标签: androidandroid-glideandroid-image

解决方案


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

如果您尝试直接使用drawable 中的新图像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()

推荐阅读