Glide探究一 如何计算加载后的图片尺寸?
一、疑问
- 调用
RequestBuilder
into(@NonNull ImageView view)
,加载后的图片尺寸是多少? - 调用
RequestBuilder
into(@NonNull Y target)
,加载后的图片尺寸是多少? - 调用
RequestOptions
override(int width, int height)
,加载后的图片尺寸是多少?
二、请求尺寸
无论使用何种方式加载图片,Glide
首先计算request
请求尺寸。请求尺寸可以在2个地方进行设置。
Target
RequestOptions
通过RequestBuilder
into(@NonNull ImageView view)
,实质上是通过Target
实现的,确切地说是BitmapImageViewTarget
或者DrawableImageViewTarget
。Target
接口中提供了一个常量SIZE_ORIGINAL
用于将请求尺寸设置为图片尺寸。
int SIZE_ORIGINAL = Integer.MIN_VALUE;
其次,可以在RequestOptions
中通过override(int width, int height)
覆盖Target
地请求尺寸。
三、三种尺寸
- request请求尺寸(在
Target
或者RequestOptions
中设置的尺寸) - source源图片尺寸(图片自身尺寸)
- target目标尺寸(加载后的图片尺寸)
上一部分中,我们已经知道如何设置Glide
的request
请求尺寸,并且图片的自身的source
源尺寸是确定的。如何通过request
和source
两种尺寸计算出最终的target
大小?答案是DownsampleStrategy
。
四、DownsampleStrategy
DownsampleStrategy
定义了2个抽象方法
public abstract float getScaleFactor(
int sourceWidth, int sourceHeight,
int requestedWidth, int requestedHeight);