当前位置: 移动技术网 > IT编程>移动开发>Android > Android小知识之图片的3种压缩方式小结

Android小知识之图片的3种压缩方式小结

2019年08月02日  | 移动技术网IT编程  | 我要评论

2014春晚评价,祁汉谢梦结婚照,够爱伴奏

设置图片格式

android目前常用的图片格式有png,jpeg和webp,

png:无损压缩图片格式,支持alpha通道,android切图素材多采用此格式

jpeg:有损压缩图片格式,不支持背景透明,适用于照片等色彩丰富的大图压缩,不适合logo

webp:是一种同时提供了有损压缩和无损压缩的图片格式,派生自视频编码格式vp8,从谷歌官网来看,无损webp平均比png小26%,有损的webp平均比jpeg小25%~34%,无损webp支持alpha通道,有损webp在一定的条件下同样支持,有损webp在android4.0(api 14)之后支持,无损和透明在android4.3(api18)之后支持

采用webp能够在保持图片清晰度的情况下,可以有效减小图片所占有的磁盘空间大小

android中bitmap所占内存大小计算方式:图片长度 x 图片宽度 x 一个像素点占用的字节数

1、bitmap的compress方法(质量压缩):

public boolean compress(compressformat format, int quality, outputstream stream)

参数format:表示图像的压缩格式,目前有compressformat.jpeg、compressformat.png、compressformat.webp。

参数quality: 图像压缩率,0-100。 0 压缩100%,100意味着不压缩。

参数stream: 写入压缩数据的输出流。

常用的用法:

public static bitmap compress(bitmap bitmap){

 bytearrayoutputstream baos = new bytearrayoutputstream();

 bitmap.compress(bitmap.compressformat.jpeg, 90, baos);

 byte[] bytes = baos.tobytearray();

 return bitmapfactory.decodebytearray(bytes, 0, bytes.length);

}

上面方法中通过bitmap的compress方法对bitmap进行质量压缩,10%压缩,90%不压缩。

图片的大小是没有变的,因为质量压缩不会减少图片的像素,它是在保持像素的前提下改变图片的位深及透明度等,来达到压缩图片的目的,这也是为什么该方法叫质量压缩方法。图片的长,宽,像素都不变,那么bitmap所占内存大小是不会变的。

quality值越小压缩后的baos越小(使用场景:在微信分享时,需要对图片的字节数组大小进行限制,这时可以使用bitmap的compress方法对图片进行质量压缩)。

2、bitmapfactory.options的injustdecodebounds和insamplesize参数(采样压缩率):

injustdecodebounds:当injustdecodebounds设置为true的时候,bitmapfactory通过decodexxxx解码图片时,将会返回空(null)的bitmap对象,这样可以避免bitmap的内存分配,但是它可以返回bitmap的宽度、高度以及mimetype。

insamplesize: 当它小于1的时候,将会被当做1处理,如果大于1,那么就会按照比例(1 / insamplesize)缩小bitmap的宽和高、降低分辨率,大于1时这个值将会被处置为2的倍数。例如,width=100,height=100,insamplesize=2,那么就会将bitmap处理为,width=50,height=50,宽高降为1 / 2,像素数降为1 / 4。

常用用法:

public static bitmap insamplesize(bitmap bitmap,int reqwidth,int reqheight){

 final bitmapfactory.options options = new bitmapfactory.options();

 options.injustdecodebounds = true;

 bitmapfactory.decodebytearray(data, 0, data.length, options);

 options.insamplesize = calculateinsamplesize(options, reqwidth,

   reqheight);

 options.injustdecodebounds = false;

 return bitmapfactory.decodebytearray(data, 0, data.length, options);

}

public static int calculateinsamplesize(bitmapfactory.options options,

          int reqwidth, int reqheight) {

 final int picheight = options.outheight;

 final int picwidth = options.outwidth;

 int targetheight = picheight;

 int targetwidth = picwidth;

 int insamplesize = 1;

 if (targetheight > reqheight || targetwidth > reqwidth) {

  while (targetheight >= reqheight

    && targetwidth >= reqwidth) {

   insamplesize += 1;

   targetheight = picheight / insamplesize;

   targetwidth = picwidth / insamplesize;

  }

 }

 return insamplesize;

}
}

insamplesize方法中先将injustdecodebounds设置为false,在通过bitmapfactory的decodexxxx方法解码图片,返回空(null)的bitmap对象,同时获取了bitmap的宽高,再通过calculateinsamplesize方法根据原bitmap的 宽高和目标宽高计算出合适的insamplesize,最后将injustdecodebounds设置为true,通过bitmapfactory的decodexxxx方法解码图片(使用场景:比如读取本地图片时,防止bitmap过大导致内存溢出)。

3、通过matrix压缩图片

matrix matrix = new matrix();

matrix.setscale(0.5f, 0.5f);

bm = bitmap.createbitmap(bit, 0, 0, bit.getwidth(),bit.getheight(), matrix, true);

}

使用场景:自定义view时,对图片进行缩放、旋转、位移以及倾斜等操作,常见的就是对图片进行缩放处理,以及圆角图片等。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对移动技术网的支持。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网