如果图像大于 Glide 中的请求大小,如何只缩小图像?

How to only scale-down an image if it is larger than requested size in Glide?

我使用 Picasso 已经很久了。今天,我要迁移到 Glide。在 Picasso 中,我曾经使用以下加载模式:

Picasso.get()
    .load(file)
    .resize(targetSize, 0)
    .onlyScaleDown()
    .placeholder(R.color.default_surface)
    .error(R.color.default_surface_error)
    .into(imageView)

根据 resize(int, int) 文档,

Use 0 as desired dimension to resize keeping aspect ratio

根据 onlyScaleDown() 文档,

Only resize an image if the original image size is bigger than the target size specified by resize(int, int)

这是我正在尝试的:

Glide.with(imageView)
    .log(this, thumbnailUrl?.toString())
    .load(thumbnailUrl)
    .override(600)
    .placeholder(R.color.default_surface)
    .error(R.color.default_surface_error)
    .into(imageView)

Glide 在使用 DownsampleStrategy.CENTER_OUTSIDE 加载图像时使用默认的下采样策略。它说图像被放大以匹配覆盖的大小,使得其中一个维度(最小?)等于覆盖的大小。并且,以下评论:

Scales, maintaining the original aspect ratio, so that one of the image's dimensions is exactly equal to the requested size and the other dimension is greater than or equal to the requested size.

This method will upscale if the requested width and height are greater than the source width and height. To avoid upscaling, use {@link #AT_LEAST}, {@link #AT_MOST}, or {@link #CENTER_INSIDE}.

DownsampleStrategy.java 中的选项让我感到困惑。我不知道我应该使用哪一个。我希望大图像缩小到覆盖大小,而小图像永远不会放大。如何在 Glide 中实现这一点?

我在 Github Issue #3215 中找到了答案,建议如下:

Pick a useful DownsampleStrategy, in particular CENTER_INSIDE may be what you're looking for. The default DownsampleStrategy will upscale to maximimize Bitmap re-use, but typically there's an equivalent strategy available that will not upscale.

而且,DownsampleStrategy.CENTER_INSIDE符合我的要求:

Returns the original image if it is smaller than the target, otherwise it will be downscaled maintaining its original aspect ratio, so that one of the image's dimensions is exactly equal to the requested size and the other is less or equal than the requested size. Does not upscale if the requested dimensions are larger than the original dimensions.

我对代码中 DownsampleStrategy.CENTER_INSIDE 的文档感到困惑。