android - 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。它是否在进行其他压缩?
解决方案
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()
推荐阅读
- react-native - 反应导航 v5 很慢,因为在打开抽屉之前重新渲染当前屏幕
- php - 滑块插件中的wordpress短代码未以模式显示
- vba - 在 VBA 中使用 selenium 仅截取浏览器的一部分 “硒 VBA”
- javascript - 如果用户在之后立即滚动,页面会跳回到滚动到视图中的元素
- google-cloud-platform - Cloud Run - 允许用户根据权限部署单个资源
- flutter - Flutter:修改插件的 podspec 后如何安装新的 pod?
- java - UrlBuilder 查询字符串编码不起作用
- flask - Flask:使用请求测试装饰器
- java - Java ServerSocket 禁止 IP 地址
- javascript - 获取 HTML 输入的字符串内容