Используйте CustomTarget из Glide, чтобы передать размеры представления изображения, чтобы Glide мог уменьшить изображения до указанного размера.
Glide.with(this)
.load(IMAGE_URL)
.into(object : CustomTarget<Drawable>(targetImageWidth, targetImageHeight) {
override fun onLoadCleared(placeholder: Drawable?) {
// called when imageView is cleared. If you are referencing the bitmap
// somewhere else too other than this imageView clear it here
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
image.setImageDrawable(resource)
}
})
Но что если вы знаете только одно измерение целевого контейнера и не знаете соотношение сторон изображения? Вы вынуждены использовать исходный размер изображения? Оказывается, может быть способ обойти это. В этом случае просто установите другое измерение, которое вы не знаете, равным 1, и Glide автоматически уменьшит изображение.
imageView.viewTreeObserver.addOnGlobalLayoutListener {
Glide.with(this)
.load(TargetActivity.IMAGE_URL)
.into(object : CustomTarget<Drawable>(imageView.width, 1) {
// imageView width is 1080, height is set to wrap_content
override fun onLoadCleared(placeholder: Drawable?) {
// clear resources
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
// bitmap will have size 1080 x 805 (original: 1571 x 1171)
imageView.setImageDrawable(resource)
}
})
}