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
地请求尺寸。
Target
或者RequestOptions
中设置的尺寸)上一部分中,我们已经知道如何设置Glide
的request
请求尺寸,并且图片的自身的source
源尺寸是确定的。如何通过request
和source
两种尺寸计算出最终的target
大小?答案是DownsampleStrategy
。
DownsampleStrategy
定义了2个抽象方法
public abstract float getScaleFactor(
int sourceWidth, int sourceHeight,
int requestedWidth, int requestedHeight);
public abstract SampleSizeRounding getSampleSizeRounding(
int sourceWidth, int sourceHeight,
int requestedWidth, int requestedHeight);
getScaleFactor
根据source
和request
大小计算出缩放比例来确定target
尺寸。
getSampleSizeRounding
用于计算inSampleSize
的数值,SampleSizeRounding
有2个枚举值。
MEMORY
内存优先,增加inSampleSize
值,保证较小的内存使用。QUALITY
质量优先,降低inSampleSize
值,保证较高的图片质量。由DownsampleStrategy
的定义可以看出,Glide
加载后的图片保持了原始比例。
通过DownsmapleStrategy
的getScaleFactor
方法,计算出scaleFactor
缩放比例。从而计算出最终的target
尺寸。
targetWidth = sourceWidth * scaleFactor;
targetHeight = sourceHeight * scaleFactor;
接下来通过BitmapFactory
加载图片即可。与加载图片相关的几个重要参数
public boolean inJustDecodeBounds; // 仅解码图片尺寸,并不加载图片
public Bitmap inBitmap; // 复用已有的Bitmap,Glide总是优先使用inBitmap复用图片
public int inSampleSize; // 采样大小,必须是2的指数,<= 1,2,4,8,16,……
public boolean inScaled; // 缩放,结合inTargetDensity和inDensity确定最终图片大小
public int inTargetDensity; // 目标Density
public int inDensity; // 源Density
BitmapFactory.Options
几个参数对加载后Bitmap
尺寸的影响。
// inScaled = false,inTargetDensity = inDensity = 0时
// 采样值越大,得到的Bitmap尺寸越小
bitmap.width = sourceWidth / inSampleSize;
bitmap.height = sourceHeight / inSampleSize;
// 同时对图片进行缩放
bitmap.width = sourceWidth / inSampleSize * inTargetDensity / inDensity;
bitmap.height = sourceHeight / inSampleSize * inTargetDensity / inDensity;
Downsampler
完成了图片的加载过程。
public Resource<Bitmap> decode(
InputStream is,
int requestedWidth, int requestedHeight,
Options options,
DecodeCallbacks callbacks) throws IOException; // 注意request大小
private Bitmap decodeFromWrappedStreams(
InputStream is,
BitmapFactory.Options options,
DownsampleStrategy downsampleStrategy,
DecodeFormat decodeFormat, boolean isHardwareConfigAllowed,
int requestedWidth, int requestedHeight,
boolean fixBitmapToRequestedDimensions,
DecodeCallbacks callbacks) throws IOException; // 出现了downsampleStrategy
private static void calculateScaling(
ImageType imageType,
InputStream is,
DecodeCallbacks decodeCallbacks,
BitmapPool bitmapPool,
DownsampleStrategy downsampleStrategy,
int degreesToRotate,
int sourceWidth,
int sourceHeight,
int targetWidth,
int targetHeight,
BitmapFactory.Options options) throws IOException; // target大小已经确定
RequestOptions
中设置request
尺寸及DownsampleStrategy
public RequestOptions override(int width, int height);
public RequestOptions downsample(@NonNull DownsampleStrategy strategy);
BitmapFactory.Options
的4个参数inSampleSize
,inScaled
,inTargetDensity
,inDensity
共同决定了加载后的bitmap
大小;DownsampleStrategy
计算request
及source
尺寸的缩放比例,进而计算出BitmapFactory.Options
的最终参数;RequestOptions
中设置request
大小及DownsampleStrategy
;Downsampler
完成了所以计算工作,并完成加载图片的工作;