为什么需要压缩?
android中的图片是以Bitmap形式存在的,bitmap的大小和数量会直接影响到手机内存情况。不同大小的手机显示的图片也会不一样,跟分辨率、手机内存、显示的大小都有关系,所以很多时候我们需要对位图进行一些压缩的操作。
计算bitmap大小
宽 高 一个像素点占用字节
四种压缩格式
[图片上传失败…(image-e55c37-1535879147605)]
ARGB代表的意思是
A(透明度)
R(red红色)
G(green绿色)
B(blue蓝色)
- ALPHA_8
8位Alpha位图一个像素占用一个字节,只有透明度。A=8 - ARGB_4444
16位的ARGB位图,ARGB各占用4位总共16位,2个字节。 - ARGB_8888
16位的ARGB位图,ARGB各占用8位总共36位,4个字节。 - RGB_565
16位RGB位图没有透明度,2个字节。
1. 质量压缩方式
质量压缩是在不改变图片大小的前提下,改变图片的位深以透明度,来达到压缩的目的。所以通过这种方式压缩的话,实际上我们内存加载进来的bitmap大小也是不会改变的。
但是这种方式会改变图片的文件大小,代码中可以看到通过了byteArray来判断大小,所以适合去传递二进制的图片数据,比如微信分享图片,要传入二进制数据过去,限制32kb之内。
1 | public static Bitmap compressByQuality(Bitmap src, long maxByteSize, boolean recycle) { |
2. 采样率压缩
采样率压缩主要是通过BitmapFactory.Options类的inJustDecodeBunds标识为true,然后返回的bitmap就是一个Null对象,这样可以节约内存空间的开销。
但是这个bitmap的options是包含bitmap的宽高、MimeType参数。我们可以根据这些图片参数去设置options的 inSampleSize参数,通过这个参数我们可以实现图片大小的压缩。
主要压缩步骤
BitmapFactory.Options的 inJustDecodeBunds为true并加载图片
从BitmapFactory.Options 中取出图片的原始宽高信息,对应outWidth和outHeight
根据采样率的规格并结合目标计算出View的所需大小计算出inSampleSize
BitmapFactory.Options 的 inJustDecodeBunds 参数设置为false,然后重新加载图片
1 | private Bitmap decodeSampleBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { |
3.缩放法压缩(martix)
1 | /** |
4. RGB_565压缩法
1 | /** |
5 . createScaledBitmap方式压缩
1 | /** |