Glide 怎么判断解析图片的
Glide 怎么保证ImageView宽高?
Glide 怎么判断图片旋转角度
以上三个问题是我自己在做一个图片池遇到的问题,趁机好好学习Glide
Glide解码的类在 Downsampler中,它的注释上写着:
Downsamples, decodes, and rotates images according to their exif orientation.
根据图像文件采样、解码、旋转图像
有几个重要的类:
- private final BitmapPool bitmapPool;
Bitmap池,是一个LruCache,内部保留着使用过的Bitmap,重复使用
- private final DisplayMetrics displayMetrics;
屏幕信息
- private final ArrayPool byteArrayPool;
同样是一个LruCache,最大是4MB,充当一个byte的buffer
- private final List parsers;
图像的头部信息解析器,例如图像类型、旋转角度等信息;
ImageHeaderParser有两个实现类:DefaultImageHeaderParser、 ExifInterfaceImageHeaderParser
DefaultImageHeaderParser是Glide自定义的默认的头部解析器
ExifInterfaceImageHeaderParser是支持SDK 27的基于 exifInterface的解析器
- private final HardwareConfigState hardwareConfigState = HardwareConfigState.getInstance();
目测跟硬件加速有关,具体还不清楚
计算图像大小、缩放、计算角度、解码、旋转
public Resource<Bitmap> decode(InputStream is, int requestedWidth, int requestedHeight,
Options options, DecodeCallbacks callbacks) throws IOException {
// 以下具体是做一系列的BitmapFactory.Options的配置
byte[] bytesForOptions = byteArrayPool.get(ArrayPool.STANDARD_BUFFER_SIZE_BYTES, byte[].class);
BitmapFactory.Options bitmapFactoryOptions = getDefaultOptions(); // 获取Option
bitmapFactoryOptions.inTempStorage = bytesForOptions; // 设定一个可重复使用的临时存储空间
DecodeFormat decodeFormat = options.get(DECODE_FORMAT);
DownsampleStrategy downsampleStrategy = options.get(DownsampleStrategy.OPTION);
boolean fixBitmapToRequestedDimensions = options.get(FIX_BITMAP_SIZE_TO_REQUESTED_DIMENSIONS);
boolean isHardwareConfigAllowed =
options.get(ALLOW_HARDWARE_CONFIG) != null && options.get(ALLOW_HARDWARE_CONFIG); // 是否允许硬件加速
try {
// 进行解码流程
Bitmap result = decodeFromWrappedStreams(is, bitmapFactoryOptions,
downsampleStrategy, decodeFormat, isHardwareConfigAllowed, requestedWidth,
requestedHeight, fixBitmapToRequestedDimensions, callbacks);
return BitmapResource.obtain(result, bitmapPool);
} finally {
releaseOptions(bitmapFactoryOptions);
byteArrayPool.put(bytesForOptions);
}
}
// 真正解码的流程
private Bitmap decodeFromWrappedStreams(InputStream is,
BitmapFactory.Options options, DownsampleStrategy downsampleStrategy,
DecodeFormat decodeFormat, boolean isHardwareConfigAllowed, int requestedWidth,
int requestedHeight, boolean fixBitmapToRequestedDimensions,
DecodeCallbacks callbacks) throws IOException {
//1. 计算图像大小
int[] sourceDimensions = getDimensions(is, options, callbacks, bitmapPool);
int sourceWidth = sourceDimensions[0];
int sourceHeight = sourceDimensions[1];
String sourceMimeType = options.outMimeType;
if (sourceWidth == -1 || sourceHeight == -1) {
isHardwareConfigAllowed = false;
}
//2. 计算图像角度
int orientation = ImageHeaderParserUtils.getOrientation(parsers, is