Log.w(TAG, “Cannot reset the input stream”, e);
}
}
}
…
}
紧接着就用到了 mark / reset 功能 , 读取头部信息里面存储的方向信息
关于 orientation 值代表的详细含义参考这篇文章: EXIF 方向参数 Orientation
// DownSampler.decode
final int[] inDimens = getDimensions(invalidatingStream, bufferedStream, options);
final int inWidth = inDimens[0];
final int inHeight = inDimens[1];
public int[] getDimensions(MarkEnforcingInputStream is, RecyclableBufferedInputStream bufferedStream,
BitmapFactory.Options options) {
options.inJustDecodeBounds = true;
decodeStream(is, bufferedStream, options);
options.inJustDecodeBounds = false;
return new int[] { options.outWidth, options.outHeight };
}
这里获取了待解析图片的宽高
计算缩放比例
// DownSampler.decode
// 计算图片被旋转的角度
final int degreesToRotate = TransformationUtils.getExifOrientationDegrees(orientation);
final int sampleSize = getRoundedSampleSize(degreesToRotate, inWidth, inHeight, outWidth, outHeight);
private int getRoundedSampleSize(int degreesToRotate, int inWidth, int inHeight, int outWidth, int outHeight) {
int targetHeight = outHeight == Target.SIZE_ORIGINAL ? inHeight : outHeight;
int targetWidth = outWidth == Target.SIZE_ORIGINAL ? inWidth : outWidth;
final int exactSampleSize;
if (degreesToRotate == 90 || degreesToRotate == 270) {
// 90 和 270 度 需要把长宽对调来计算缩放比例
exactSampleSize = getSampleSize(inHeight, inWidth, targetWidth, targetHeight);
} else {
exactSampleSize = getSampleSize(inWidth, inHeight, targetWidth, targetHeight);
}
// 去一个最大的 且 <= exactSampleSize 且 是2的次方
final int powerOfTwoSampleSize = exactSampleSize == 0 ? 0 :
Integer.highestOneBit(exactSampleSize);
// powerOfTwoSampleSize == 0 代表不缩放,也就是返回 1 倍
return Math.max(1, powerOfTwoSampleSize);
}
public static final Downsampler AT_LEAST = new Downsampler() {
@Override
protected int getSampleSize(int inWidth, int inHeight, int outWidth, int outHeight) {
// 按照目标的长宽比判定 至少需要缩放多少倍
return Math.min(inHeight / outHeight, inWidth / outWidth);
}
@Override
public String getId() {
return “AT_LEAST.com.bumptech.glide.load.data.bitmap”;
}
};
public static final Downsampler AT_MOST = new Downsampler() {
@Override
protected int getSampleSize(int inWidth, int inHeight, int outWidth, int outHeight) {
// 按照目标的长宽比判定 至多需要缩放多少倍
int maxIntegerFactor = (int) Math.ceil(Math.max(inHeight / (float) outHeight,
inWidth / (float) outWidt
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整资料开源分享
h));
int lesserOrEqualSampleSize = Math.max(1, Integer.highestOneBit(maxIntegerFactor));
return lesserOrEqualSampleSize << (lesserOrEqualSampleSize < maxIntegerFactor ? 1 : 0);
}
@Override
public String getId() {
return “AT_MOST.com.bumptech.glide.load.data.bitmap”;
}
};
先根据目标宽高算出整数的缩放比例, 有两种计算方式(但其实查看 3.7.0的代码, 只用到了 AT_LEAST)
- AT_LEAST 取 sampleSize 的最小值, 意思是: 至少需要缩放多少倍
- AT_MOST 取 sampleSize 的最大值 (而且还是 ceil 的方式向上取整) 意思是最多需要缩放多少倍
这步计算我们拿到的 exactSampleSize 却不是最终的 sampleSize, 官文里面有提到, sampleSize 需要是 2 的整数次方 且 大于一, 所以我们需要在exactSampleSize 范围内找一个最大的满足 2 的整数次方
的最终 sampleSize , 并且与 1 进行比较
sampleSize == 4 代表缩小 4 倍
缩放比例就计算完了, 下一步
解析原图为对应缩放比例
// DownSampler.decode
final Bitmap downsampled = downsampleWithSize(invalidatingStream, bufferedStream,
options, pool, inWidth, inHeight,
sampleSize,decodeFormat);
private Bitmap downsampleWithSize(MarkEnforcingInputStream is, RecyclableBufferedInputStream bufferedStream,
BitmapFactory.Options options, BitmapPool pool, int inWidth, int inHeight, int sampleSize,
DecodeFormat decodeFormat) {
// 读取 config
Bitmap.Config config = getConfig(is, decodeFormat);
// 初始化 options
options.inSampleSize = sampleSize;
options.inPreferredConfig = config;
// 这里利用 BitmapPool 对象池 和 Bitmap 的重用机制, 做了一个Bitmap内存重用的东东
if ((options.inSampleSize == 1 || Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) && shouldUsePool(is)) {
int targetWidth = (int) Math.ceil(inWidth / (double) sampleSize);
int targetHeight = (int) Math.ceil(inHeight / (double) sampleSize);
setInBitmap(options, pool.getDirty(targetWidth, targetHeight, config));
}
// 开始解析
return decodeStream(is, bufferedStream, options);
}
private static void setInBitmap(BitmapFactory.Options options, Bitmap recycled) {
if (Build.VERSION_CODES.HONEYCOMB <= Build.VERSION.SDK_INT) {
// 给 options 的 inBitmap 字段赋值, 可以将原始图片资源解析到一个不用的 Bitmap 对象中去
options.inBitmap = recycled;
}
}
private static Bitmap decodeStream(MarkEnforcingInputStream is, RecyclableBufferedInputStream bufferedStream,
BitmapFactory.Options options) {
…
final Bitmap result = BitmapFactory.decodeStream(is, null, options);
…
return result;
}
- 读取 config
- 把 config 和之前算好的 sampleSize 赋值给 options
- 给 options 的 inBitmap 字段赋值, 可以将原始图片资源解析到一个不用的 Bitmap 对象中去
- 利用 Bitmap 的资源重用机制完成对原始图片的解码操作
关于Bitmap的重用机制可以参考, Android Bitmap(一), 资源重用 关于对象池的概念可以参考, Android Glide 3.7.0 源码解析(四) , BitmapPool作用及原理
解码过程分析完毕, 接下来的图形转换就很简单了, 一共就两个函数
三、图形转换 fitCenter
public class FitCenter extends BitmapTransformation {
public FitCenter(Context context) {
super(context);
}
public FitCenter(BitmapPool bitmapPool) {
super(bitmapPool);
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
// 关键代码在这里
return TransformationUtils.fitCenter(toTransform, pool, outWidth, outHeight);
}
@Override
public String getId() {
return “FitCenter.com.bumptech.glide.load.resource.bitmap”;
}
}
// TransformationUtils
public static Bitmap fitCenter(Bitmap toFit, BitmapPool pool, int width, int height) {
if (toFit.getWidth() == width && toFit.getHeight() == height) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, “requested target size matches input, returning input”);
}
return toFit;
}
// 计算缩放比例 2 代表放大 2 倍, 这里去最小值, 意思是保证能放的进去 ImageView 控件
final float widthPercentage = width / (float) toFit.getWidth();
final float heightPercentage = height / (float) toFit.getHeight();
final float minPercentage = Math.min(widthPercentage, heightPercentage);
final int targetWidth = (int) (minPercentage * toFit.getWidth());
final int targetHeight = (int) (minPercentage * toFit.getHeight());
if (toFit.getWidth() == targetWidth && toFit.getHeight() == targetHeight) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, “adjusted target size matches input, returning input”);
}
return toFit;
}
// 对象池获取一个旧的大小匹配的
Bitmap.Config config = getSafeConfig(toFit);
Bitmap toReuse = pool.get(targetWidth, targetHeight, config);
if (toReuse == null) {
toReuse = Bitmap.createBitmap(targetWidth, targetHeight, config);
}
// 设置透明属性, 官文描述在某些情况下可以提升Bitmap的绘制速度
TransformationUtils.setAlpha(toFit, toReuse);
// 矩阵变换控制缩放
Canvas canvas = new Canvas(toReuse);
Matrix matrix = new Matrix();
matrix.setScale(minPercentage, minPercentage);
Paint paint = new Paint(PAINT_FLAGS);
canvas.drawBitmap(toFit, matrix, paint);
return toReuse;
}
以上源码,所见即所得,非常简单, 需要注意的是
- 缩放比例选取最小值, 是为的能放的进去界面组件, 因为是FitCenter
- TransformationUtils.setAlpha 设置是否包含透明像素的标志位, 某些情况下可以提升绘制速度,查看官文描述如下
四、图形转换 centerCrop
// CenterCrop
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
final Bitmap toReuse = pool.get(outWidth, outHeight, toTransform.getConfig() != null
? toTransform.getConfig() : Bitmap.Config.ARGB_8888);
// 还是在 TransformationUtils 中进行处理
Bitmap transformed = TransformationUtils.centerCrop(toReuse, toTransform, outWidth, outHeight);
if (toReuse != null && toReuse != transformed && !pool.put(toReuse)) {
toReuse.recycle();
}
return transformed;
}
public static Bitmap centerCrop(Bitmap recycled, Bitmap toCrop, int width, int height) {
if (toCrop == null) {
return null;
} else if (toCrop.getWidth() == width && toCrop.getHeight() == height) {
return toCrop;
}
final float scale;
float dx = 0, dy = 0;
Matrix m = new Matrix();
// 这个算式改成除法比较好理解 toCrop.getWidth()/width > toCrop.getHeight()/height, 结合 CenterCrop 的属性理解
if (toCrop.getWidth() * height > width * toCrop.getHeight()) {
// 宽度超限了,需要对宽度进行裁剪
// 按照高度比例进行缩放
scale = (float) height / (float) toCrop.getHeight();
dx = (width - toCrop.getWidth() * scale) * 0.5f;
} else {
// 高度超限了,需要对高度进行裁剪
// 按照宽度比例进行缩放
scale = (float) width / (float) toCrop.getWidth();
dy = (height - toCrop.getHeight() * scale) * 0.5f;
}
// 先缩放
m.setScale(scale, scale);
// 再平移
height / (float) toCrop.getHeight();
dx = (width - toCrop.getWidth() * scale) * 0.5f;
} else {
// 高度超限了,需要对高度进行裁剪
// 按照宽度比例进行缩放
scale = (float) width / (float) toCrop.getWidth();
dy = (height - toCrop.getHeight() * scale) * 0.5f;
}
// 先缩放
m.setScale(scale, scale);
// 再平移
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)