Android Glide 3,338页网易Android面试真题解析火爆全网

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() {

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值