前言
Glide归根结底是一个图片加载框架,它一定会涉及到BitmapFactory
相关API把Bitmap
读取到内存;可能大家已经很熟悉如何高效的加载Bitmap(比如使用inSample等),这一章还是要看一看Glide是如何玩转的;
本文主要分析这两个类:
DownsampleStrategy
Downsampler
从”Glide会对原图进行放大”案例开始
假设我们有一张宽高100x200的网络图片,需要加载到300x300像素的ImageView上(scaleType属性为CenterCrop),用Glide加载,不做任何处理会得到多大的Bitmap?
1 | Glide.with(MainActivity.this).load(URL).listener(new RequestListener<Drawable>() { |
不出意外会得到300*300的Bitmap,Bitmap比例显然被放大了;
我怀疑是进行了Transformation
操作,所以我们要禁用Transformation
;
假设我们对RequestOptions加上noTransformation的属性,比如这个样子:1
2RequestOptions requestOptions = RequestOptions.noTransformation();
Glide.with(MainActivity.this).load(URL).apply(requestOptions).listener(xxx).into(vh.imageView);
不出意外会得到300*600的Bitmap,Bitmap比例依然在被放大,甚至更大了;
如果我们不想让Bitmap被放大,可以用加载原图尺寸的方式,比如设置override(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)
或者用SimpleTarget
这样的Target
,但是加载原图终究不是解决方案,为什么?内存的原因,大部分情况下我们还是希望Glide来对图片进行缩小,加载原图的操作等于是直接把APP往OOM送近了一步;
Bitmap被放大不是Glide发明的,是Android官方带的头,比如同一张图片,放在res中不同drawable文件夹下,得到的尺寸不一样;既然如此,Bitmap被放大肯定是有积极意义的,也难怪Glide很少提及这事;
但是,总会有强迫症患者不想Bitmap被放大,Glide肯定也想到了这一点,具体怎么限制Bitmap不会放大,答案的使用RequestOptions.downsample()方法
;
RequestOptions.downsample()
方法接受参数类型为:DownsampleStrategy
,那么我们从DownsampleStrategy
开始分析;
DownsampleStrategy
DownsampleStrategy
顾名思义就是下采样策略,下采样是图像进行缩小的一种方式;
DownsampleStrategy.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16public abstract class DownsampleStrategy {
//获取缩放比例
public abstract float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
int requestedHeight);
//获取SampleSize策略
public abstract SampleSizeRounding getSampleSizeRounding(int sourceWidth, int sourceHeight,
int requestedWidth, int requestedHeight);
}
public enum SampleSizeRounding {
//内存优先
MEMORY,
//图片质量优先
QUALITY,
}
DownsampleStrategy
是抽象类,提供两个抽象方法,getScaleFactor()
顾名思义是获取缩放比例的,getSampleSizeRounding()
可能是获取SampleSize的,DownsampleStrategy
真是的实现类在其内部,几个嵌套内部类FitCenter
,CenterOutside
,AtLeast
,AtMost
,None
,CenterInside
,除此之外,DownsampleStrategy
还定义对应类的静态变量;
DownsampleStrategy.java1
2
3
4
5
6
7
8
9
10
11
12
13public static final DownsampleStrategy FIT_CENTER = new FitCenter();
public static final DownsampleStrategy CENTER_OUTSIDE = new CenterOutside();
public static final DownsampleStrategy AT_LEAST = new AtLeast();
public static final DownsampleStrategy AT_MOST = new AtMost();
public static final DownsampleStrategy CENTER_INSIDE = new CenterInside();
public static final DownsampleStrategy NONE = new None();
public static final DownsampleStrategy DEFAULT = CENTER_OUTSIDE;
其中DEFAULT
应该是Glide默认的策略,它指向的是CENTER_OUTSIDE
,我们简单看一下CenterOutside
的逻辑;
CenterOutside.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22private static class CenterOutside extends DownsampleStrategy {
@Synthetic
CenterOutside() { }
@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
int requestedHeight) {
//宽度比例(控件/图片)
float widthPercentage = requestedWidth / (float) sourceWidth;
//高度比例(控件/图片)
float heightPercentage = requestedHeight / (float) sourceHeight;
//谁大取谁
return Math.max(widthPercentage, heightPercentage);
}
@Override
public SampleSizeRounding getSampleSizeRounding(int sourceWidth, int sourceHeight,
int requestedWidth, int requestedHeight) {
return SampleSizeRounding.QUALITY;
}
}
主要关注是CenterOutside
的getScaleFactor()
逻辑,该逻辑非常简单,主要是拿控件宽高/图片宽高
,得到尺寸取最大值,我们回到文章开头的那个例子,假设此时控件宽高是300x300,图片宽高100x200:
widthPercentage = 300/100 = 3.0f;
heightPercentage = 300/200 = 1.5f;
Math.max(widthPercentage, heightPercentage) = 3.0f;
从这个推理来看,文章开头那个图片确实会被放大3倍;
文章开头试图找控制缩放比例不超过1的,就是只缩小不放大的,有没有这样的策略,其实是有的,比如AtLeast
,AtMost
;
AtLeast.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18private static class AtLeast extends DownsampleStrategy {
@Synthetic
AtLeast() { }
@Override
public float getScaleFactor(int sourceWidth, int sourceHeight, int requestedWidth,
int requestedHeight) {
int minIntegerFactor = Math.min(sourceHeight / requestedHeight, sourceWidth / requestedWidth);
return minIntegerFactor == 0 ? 1f : 1f / Integer.highestOneBit(minIntegerFactor);
}
@Override
public SampleSizeRounding getSampleSizeRounding(int sourceWidth, int sourceHeight,
int requestedWidth, int requestedHeight) {
return SampleSizeRounding.QUALITY;
}
}
AtLeast
对getScaleFactor
的逻辑简要分析:
获取图片宽高/布局宽高的比例的最小值,转成int值;
如果这个值等于0,直接返回1,等于0其实就意味着图片宽高/布局宽高至少有一个是小于1的,也就屏蔽了需要放大的情况;
如果minIntegerFactor
不等一0,肯定是需要缩小的,最后返回1f / Integer.highestOneBit(minIntegerFactor)
,其中Integer.highestOneBit(minIntegerFactor)
是取minIntegerFactor
的二进制形式最左边的最高一位且高位后面全部补零,最后返回int型的结果;
其余的DownsampleStrategy
实现类就不讲解了,DownsampleStrategy
是负责计算缩放比例和SampleSize策略,那么真正去进行Bitmap计算的是Downsampler
;
重头戏Downsampler
Downsampler.java
Downsampler
这个类在我们之前分析Decode流程时已经遇到过它,该类的主要职责是从输入流中解析出Bitmap
,核心功能的入口方法在decode()
;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31public Resource<Bitmap> decode(InputStream is, int requestedWidth, int requestedHeight,
Options options, DecodeCallbacks callbacks) throws IOException {
Preconditions.checkArgument(is.markSupported(), "You must provide an InputStream that supports"
+ " mark()");
byte[] bytesForOptions = byteArrayPool.get(ArrayPool.STANDARD_BUFFER_SIZE_BYTES, byte[].class);
//获取默认的BitmapFactory
BitmapFactory.Options bitmapFactoryOptions = getDefaultOptions();
bitmapFactoryOptions.inTempStorage = bytesForOptions;
//decodeFormat主要是RGB_8888||RGB_565
DecodeFormat decodeFormat = options.get(DECODE_FORMAT);
//获取DownsampleStrategy
DownsampleStrategy downsampleStrategy = options.get(DownsampleStrategy.OPTION);
//是否fixedBitmapSize
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
Bitmap result = decodeFromWrappedStreams(is, bitmapFactoryOptions,
downsampleStrategy, decodeFormat, isHardwareConfigAllowed, requestedWidth,
requestedHeight, fixBitmapToRequestedDimensions, callbacks);
//BitmapResource包装Bitmap
return BitmapResource.obtain(result, bitmapPool);
} finally {
releaseOptions(bitmapFactoryOptions);
byteArrayPool.put(bytesForOptions);
}
}
首先调用getDefaultOptions()
获取默认的BitmapFactory
;
默认的BitmapFactory
默认的BitmapFactory使用一个队列缓存,初始化设置在resetOptions()
方法;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15private static void resetOptions(BitmapFactory.Options decodeBitmapOptions) {
decodeBitmapOptions.inTempStorage = null;
decodeBitmapOptions.inDither = false;
decodeBitmapOptions.inScaled = false;
decodeBitmapOptions.inSampleSize = 1;
decodeBitmapOptions.inPreferredConfig = null;
decodeBitmapOptions.inJustDecodeBounds = false;
decodeBitmapOptions.inDensity = 0;
decodeBitmapOptions.inTargetDensity = 0;
decodeBitmapOptions.outWidth = 0;
decodeBitmapOptions.outHeight = 0;
decodeBitmapOptions.outMimeType = null;
decodeBitmapOptions.inBitmap = null;
decodeBitmapOptions.inMutable = true;
}
继续decode()
方法分析,首先从Option
中获取decodeFormat
,fixBitmapToRequestedDimensions
,isHardwareConfigAllowed
等,这个Option
是从RequestOptions
传递过来,代表用户的配置;
decodeFormat
表示解析格式,RGB_565或ARGB_8888;fixBitmapToRequestedDimensions
表示是否填充尺寸,不进行缩放isHardwareConfigAllowed
是否允许硬件位图,详细了解可以看Glide官方文档:https://muyangmin.github.io/glide-docs-cn/doc/hardwarebitmaps.html;- 用
BitmapResource
包装Bitmap
返回;
真正的加载Bitmap逻辑在decodeFromWrappedStreams()
方法:
decodeFromWrappedStreams
decodeFromWrappedStreams()
1 | private Bitmap decodeFromWrappedStreams(InputStream is, |
decodeFromWrappedStreams()
方法首先调用getDimensions()
获取输入流图片尺寸和mimeType
;- 获取图片的方向和旋转角度;
- 确认目标控件的宽高,获取图片的ImageType;
- 调用
calculateScaling()
计算缩放信息; - 调用
calculateConfig()
计算其他配置信息; - 根据配置,调用
setInBitmap()
设置inBitmap; - 上诉流程完成
BitmapFactory.Option
配置,调用decodeStream()
解析输入流; - 得到Bitmap,对Bitmap做最后的操作;
getDimensions()1
2
3
4
5
6
7
8
9private static int[] getDimensions(InputStream is, BitmapFactory.Options options,
DecodeCallbacks decodeCallbacks, BitmapPool bitmapPool) throws IOException {
//只解析Bounds
options.inJustDecodeBounds = true;
decodeStream(is, options, decodeCallbacks, bitmapPool);
//重置为false;
options.inJustDecodeBounds = false;
return new int[] { options.outWidth, options.outHeight };
}
getDimensions()
使用options.inJustDecodeBounds = true
读取输入流图片信息;
计算缩放
calculateScaling()
1 | private static void calculateScaling( |
calculateScaling代码看似复杂,但是仔细分析流程还是很清晰的,该方法主要计算BitmapFactory.Factory
的inSampleSize
和inTargetDensity
、inDensity
,inSampleSize
主要是进行向下采样,采样后自然会对图片进行缩小,但是可能不满足目标缩放比例,所以再配合inTargetDensity
、inDensity
进行二次缩放;主要针对两个缩放变量powerOfTwoSampleSize
和adjustedScaleFactor
;具体流程分析:
流程一
- 计算
powerOfTwoSampleSize
过程:powerOfTwoSampleSize
是最终赋值给options.inSampleSize
的;
- 根据图片角度不同,分别调用
downSampleStategy.getScaleFactor()
方法,得到exactScaleFactor
; - 通过
exactScaleFactor
计算出outWidth
和outHeight
,再重新计算widthScaleFactor
和heightScaleFactor
(ps:widthScaleFactor和heightScaleFactor是int类型,而exactScaleFactor是浮点型,结果是不一样的); - 通过策略得到int类型的比例
scaleFactor
- 判断是否支持下采样,计算出最终的缩放比例
powerOfTwoSampleSize
- 赋值给options.inSampleSize
流程二
- 计算
adjustedScaleFactor
过程:adjustedScaleFactor
是最终计算options.inTargetDensity
和options.inDensity
的;
- 不同格式和版本的下采样逻辑不同,Glide分别实现采样逻辑的计算,然后生成采样后的宽高尺寸
powerOfTwoWidth
和powerOfTwoHeight
; - 再此调用
downsampleStrategy.getScaleFactor()
得到缩放比adjustedScaleFactor
; - 调用
adjustTargetDensityForError()
和getDensityMultiplier()
得到inTargetDensity
和inDensity
并赋值; - 上面
inDensity
依赖options.inScaled = true
,最终还得判断是否能够进行scale;
计算硬件位图/Bitmap.Config
calculateConfig()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38private void calculateConfig(
InputStream is,
DecodeFormat format,
boolean isHardwareConfigAllowed,
boolean isExifOrientationRequired,
BitmapFactory.Options optionsWithScaling,
int targetWidth,
int targetHeight) {
//如果支持硬件位图
if (hardwareConfigState.setHardwareConfigIfAllowed(
targetWidth,
targetHeight,
optionsWithScaling,
format,
isHardwareConfigAllowed,
isExifOrientationRequired)) {
return;
}
//这种情况,直接配置ARGB_8888
if (format == DecodeFormat.PREFER_ARGB_8888
|| Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN) {
optionsWithScaling.inPreferredConfig = Bitmap.Config.ARGB_8888;
return;
}
//是否有透明的通道
boolean hasAlpha = false;
try {
hasAlpha = ImageHeaderParserUtils.getType(parsers, is, byteArrayPool).hasAlpha();
} catch (IOException e) {
}
//有透明的通道,需要配置成ARGB_8888
optionsWithScaling.inPreferredConfig =
hasAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
if (optionsWithScaling.inPreferredConfig == Config.RGB_565) {
optionsWithScaling.inDither = true;//565支持抖动界面
}
}
calculateConfig()
主要是对BitmapFactory.Option
的硬件位图和inPreferredConfig
进行计算;如果用户配置8888直接配置,如果用户配置565,还需要判断是否有透明度通道,如果有透明度通道,依然采用8888解码器;
硬件位图
HardwareConfigState.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25boolean setHardwareConfigIfAllowed(
int targetWidth,
int targetHeight,
BitmapFactory.Options optionsWithScaling,
DecodeFormat decodeFormat,
boolean isHardwareConfigAllowed,
boolean isExifOrientationRequired) {
if (!isHardwareConfigAllowed
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.O
|| isExifOrientationRequired) {
Android O以上支持
return false;
}
//对尺寸有要求;
boolean result =
targetWidth >= MIN_HARDWARE_DIMENSION
&& targetHeight >= MIN_HARDWARE_DIMENSION
&& isFdSizeBelowHardwareLimit();
if (result) {
optionsWithScaling.inPreferredConfig = Bitmap.Config.HARDWARE;
optionsWithScaling.inMutable = false;
}
return result;
}
硬件位图Bitmap.Config.HARDWARE 是一种 Android O 添加的新的位图格式。硬件位图仅在显存 (graphic memory) 里存储像素数据,并对图片仅在屏幕上绘制的场景做了优化。
###设置InBitmap
setInBitmap()1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19private static void setInBitmap(
BitmapFactory.Options options, BitmapPool bitmapPool, int width, int height) {
@Nullable Bitmap.Config expectedConfig = null;
// Avoid short circuiting, it appears to break on some devices.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//硬件位图不设置
if (options.inPreferredConfig == Config.HARDWARE) {
return;
}
//inJustDecodeBoudes时候有可能可以获取到
expectedConfig = options.outConfig;
}
if (expectedConfig == null) {
expectedConfig = options.inPreferredConfig;
}
options.inBitmap = bitmapPool.getDirty(width, height, expectedConfig);
}
设置成硬件位图的不能进行inBitmap
操作,最终会设置options.inBitmap
,其中缓存的位图从bitmapPool中获取;
解析输出流
decodeStream()
万事具体只差东风,设置完BitmapFactory.Options之后,正在解析输入流的代码就在decodeStream()
:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36private static Bitmap decodeStream(InputStream is, BitmapFactory.Options options,
DecodeCallbacks callbacks, BitmapPool bitmapPool) throws IOException {
if (options.inJustDecodeBounds) {
is.mark(MARK_POSITION);
} else {
callbacks.onObtainBounds();
}
int sourceWidth = options.outWidth;
int sourceHeight = options.outHeight;
String outMimeType = options.outMimeType;
final Bitmap result;
TransformationUtils.getBitmapDrawableLock().lock();
try {
//真正的解析调用
result = BitmapFactory.decodeStream(is, null, options);
} catch (IllegalArgumentException e) {
if (options.inBitmap != null) {
try {
//发生异常要做inBitmap回收
is.reset();
bitmapPool.put(options.inBitmap);
options.inBitmap = null;//不适用inBitmap
return decodeStream(is, options, callbacks, bitmapPool);
} catch (IOException resetException) {
throw bitmapAssertionException;
}
}
throw bitmapAssertionException;
} finally {
TransformationUtils.getBitmapDrawableLock().unlock();
}
if (options.inJustDecodeBounds) {
is.reset();
}
return result;
}
decodeStream()
核心的方法是调用BitmapFactory.decodeStream(is, null, options)
做解析工作,剩下的代码都是对异常解析,如果发现异常时inBitmap
不为空,设置inBitmap
为null
并重试一次;
总结
本文主要是对Glide下采样做了简单介绍,从代码流程上分析,可以分为对BitmapFactory.Options的配置、调用BitmapFactory解析输入流以及对Bitmap最后的处理三个步骤,其中Options配置阶段在计算缩放上最为重要,其中在缩放因子计算阶段,涉及native
以及libjpeg
底层的逻辑尤为重要,值得学习;