android图片压缩处理

一,常见图片格式基础
bmp: 
   标准的图片
jpeg:
   采用有损压缩算法压缩得到的图片,去掉了原图中的透明度
png:
   采用无损压缩算法压缩得到的图片

图片大小:
     bmp>png>jpeg

二,android中常见图片处理API:
1.Bitmap实例的compress(CompressFormat format, int quality, OutputStream os)
作用:对图片进行压缩处理放到指定输出流,图片本身在内存中的大小并没有改变,只是在压缩后的输出流中大小变小,一旦又从输出流解码得出图片,在占用的内存还是和原图片一样。
主要参数说明:
format:
的压缩的格式,可以有png,jpeg
quality:
Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting


2.BitmapFactory.decode**(**,  Options op)
作用:对图片进行解码
op常用属性有:

1.inPurgeable  (在5.0之后,包括5.0是不推荐使用的,常与inInputShareable搭配使用,是否可清除的,如果是可清除的,在再次需要图片的时候要重新进行解码)
Deprecated As of android.os.Build.VERSION_CODES.LOLLIPOP, this is ignored. In android.os.Build.VERSION_CODES.KITKAT and below, if this is set to true, then the resulting bitmap will allocate its pixels such that they can be purged if the system needs to reclaim memory. In that instance, when the pixels need to be accessed again (e.g. the bitmap is drawn, getPixels() is called), they will be automatically re-decoded.
For the re-decode to happen, the bitmap must have access to the encoded data, either by sharing a reference to the input or by making a copy of it. This distinction is controlled by inInputShareable. If this is true, then the bitmap may keep a shallow reference to the input. If this is false, then the bitmap will explicitly make a copy of the input data, and keep that. Even if sharing is allowed, the implementation may still decide to make a deep copy of the input data.
While inPurgeable can help avoid big Dalvik heap allocations (from API level 11 onward), it sacrifices performance predictability since any image that the view system tries to draw may incur a decode delay which can lead to dropped frames. Therefore, most apps should avoid using inPurgeable to allow for a fast and fluid UI. To minimize Dalvik heap allocations use the inBitmap flag instead.
Note: This flag is ignored when used with decodeResource(Resources, int, * android.graphics.BitmapFactory.Options) or decodeFile(String, * android.graphics.BitmapFactory.Options).

2.inInputShareable(在5.0之后,包括5.0是不推荐使用的,需要与inPurgeable搭配使用,决定是否能引用输入流,还是进行深拷贝)
Deprecated As of android.os.Build.VERSION_CODES.LOLLIPOP, this is ignored. In android.os.Build.VERSION_CODES.KITKAT and below, this field works in conjuction with inPurgeable. If inPurgeable is false, then this field is ignored. If inPurgeable is true, then this field determines whether the bitmap can share a reference to the input data (inputstream, array, etc.) or if it must make a deep copy.

3.inJustDecodeBounds
If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels
当设置为true的时候,不会返回bitmap对象,只能通过config对象的outHeight,outWidth属性查询到图片占用内存的大小,这个在方法可以不会在内存中加载图片,可以通过这个方法获取图片的尺寸

4.inSampleSize
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2。
设置获取到的图片与原图片的大小比例,2表示宽和高都是原来图片的1/2,即大小是原来图片的1/4,依次类推。

4.inPreferredConfig
值可以有:
ALPHA_8   图片的每个像素只有1个字节,只有透明度一个通道,这个通道由8位二进制数组成。
ARGB_4444   图片的每个像素有2个字节,由3个RGB的通道,1个透明度的通道,每个通道都有4位二进制数组成。
ARGB_8888   图片的每个像素有4个字节,由3个RGB的通道,1个透明度的通道,每个通道都有8位二进制数组成。
RGB_565       图片的每个像素有2个字节,有3个RGB的通道,其中R和B通道有5位二进制数组成,G通道有6位二进制数组成。 

你可能感兴趣的:(Android)