图片的大小=宽*高*像素点占用字节数+图片属性信息。(也是它占用内存的大小)
单色位图 | 2种颜色 | 占用1个二进制位表示1个像素点,即占用1/8字节。 | 400*800*1/8=40000+图片信息 |
16色位图 | 16种颜色 | 占用4个二进制位表示1个像素点,即占用4/8字节。 | 400*800*4/8=160000+图片信息 |
256色位图 | 156种颜色 | 占用8个二进制位表示1个像素点,即占用1字节。 | 400*800*1=320000+图片信息 |
24位位图 | 1600万种颜色 | 占用24个二进制位表示1个像素点,即占用3字节。 | 400*800*3=960000+图片信息 |
24位图表示方式:RGB#ff0000。(十六进制FF换成二进制为11111111占用一个字节,red占1字节,green占1字节,blue占1字节。Android中使用的是ARGB,alpha透明度占4位。 |
Bitmap可以获取图像文件信息,进行图像颜色变换、剪切、旋转、缩放等操作,并可以指定格式保存图像文件。Android不能直接编辑原图,因此都是对副本进行操作。
颜色配置 Bitmap.Config
ALPHA_8 | 【只有透明度】颜色信息只由透明度组成,每个像素占8位(1个字节)。 |
ARGB_4444 | 【显示不清楚】颜色信息由 A(透明度)、R(红),G(绿),B(蓝) 组成,每个像素占16位(2个字节)。 |
ARGB_8888 | 【占用内存多】颜色信息由 A(透明度)、R(红),G(绿),B(蓝) 组成,每个像素占32位(4个字节)。是Bitmap默认的颜色配置信息。 |
压缩方式 Bitmap.CompressFormat
JEPG | 以 JEPG 算法进行图像压缩,文件后缀格式为".jpg"或".jpeg",是一种有损压缩。 |
PNG | 以 PNG 算法进行图像压缩,文件后缀格式为".png",是一种无损压缩。 |
WEBP | 以 WebP 算法进行图像压缩,文件后缀格式为".webp",是一种无损压缩。(相同情况下体积比jepg小40%,但编码时间长8倍) |
形参不带Resources res的已过时,直接传 getResource( ) 就行。
BitmapDrawable(Resources res, Bitmap bitmap) |
BitmapDrawable(Resources res, java.io.InputStream is) |
BitmapDrawable(Resources res, String filepath) |
从Resource(资源)、File(本地)、Stream(网络、Assets)、ByteArray(二进制数据) 各种来源中解码(读取)位图。
public static Bitmap decodeFile(String pathName) public static Bitmap decodeFile(String pathName, Options opts) 通过文件。 |
public static Bitmap decodeStream(InputStream is) public static Bitmap decodeStream(InputStream is, Rect outPadding, Options opts) 通过输入流。 |
public static Bitmap decodeResource(Resources res, int id) public static Bitmap decodeResource(Resources res, int id, Options opts) 通过资源。 |
public static Bitmap decodeByteArray(byte[] data, int offset, int length) public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts) 通过字节数组。 |
设置选项 BitmapFactory.Options
inJustDecodeBounds | 设置为 true 只获取图片信息而不加载进内存 ,可以避免OOM。 |
inSampleSize | 对图像进行压缩(设置为4则宽高都为原来的1/4,图为原来的1/16)。 |
outWidth | 获取图片的宽度。 |
outHeight | 获取图片的高度。 |
inDensity inTargetDensity | 给位图设置的密度。 绘制到新图上的密度。 |
inScaled | 设为 true 时进行图片压缩,从 inDensity到 inTargetDensity。 |
inPreferredConfig | 颜色配置信息,默认是Bitmap.Config.ARGB_8888。 |
fun readBitmapFromFile(filePath: String, width: Int, height: Int): Bitmap {//仅获取图片尺寸,不直接加载进内存,避免大图产生OOMval options = BitmapFactory.Options()options.inJustDecodeBounds = trueBitmapFactory.decodeFile(filePath, options)val outWidth = options.outWidth.toFloat()val outHeight = options.outHeight.toFloat()//只要图片宽高大于需要的尺寸就缩放var inSampleSize = 1if (outWidth > width || outHeight > height) {//用图片最长的那个边计算缩放比例if (outWidth > outHeight) {inSampleSize = (outHeight / height).roundToInt() //四舍五入取整} else {inSampleSize = (outWidth / width).roundToInt()}}//利用计算出的缩放比例生成新图options.inJustDecodeBounds = falseoptions.inSampleSize = inSampleSizereturn BitmapFactory.decodeFile(filePath, options)
}
对现有的位图进行处理,或创建空白的。
Bitmap source | 通过一个给定的位图来生成位图(不带这个参数的重载生成的是空白的Bitmap)。 |
int x | 新图时第一个像素的横坐标。 |
int y | 新图第一个像素的纵坐标。 |
int width | 新图的宽度(若是通过原图开生成,x+width不能超过原图宽度)。 |
int height | 新图的长度(若是通过原图开生成,y+height不能超过原图长度)。 |
Matrix m | translate(平移)、rotate(旋转)、scale(缩放)、skew(错切)。 pre(队列前插入)、post(队列后追加 )、set(清空队列再添加)。 |
boolean filter | 为true时原图会被忽略,仅当矩阵不止包含移动操作的时候适用。 |
DisplayMetrics display | |
ColorSpace colorSpace | |
Config config | |
int[] colors | |
Picture source |
val matrix = Matrix().apply {postScale(0.8f, 0.9f) //缩放(四参重载的后两位表示以该点为中心)postRotate(-45.21f) //旋转(负数为逆时针)postTranslate(100f, 80f) //移动
}
Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true)
compress( ) | 将位图压缩到指定的输出流,可看作将位图保存到文件中。 format:颜色配置。 quelity:压缩质量,0-100。(PNG无损会忽略该项) |
recycle( ) | 回收位图占用的内存空间,把位图标记为Dead。 |
isRecycle( ) | 判断位图内存是否已释放。 |
getWidth( ) | 获取位图的宽度。 |
getHeight( ) | 获取位图的高度。 |
isMutable( ) | 图片是否可修改。 |
getScaledWidth( ) | 获取指定密度转换后的图像宽度。 |
getScaledHeight( ) | 获取指定密度转换后的图像高度。 |
inPreferQualityOverSpeed( ) | 为 true则保证质量大于解码速度。 |