Bitmap 简介
Bitmap 简介
Drawable, Canvas和 Bitmap区别
-
Drawable: 通用的图像对象, 用于装载常用格式的图像,如PNG、JPG、GIF等;
-
Canvas: 画布,可绘制的区域。提供drawXXX()等方法将Text、Bitmap图形绘制到画布上;
-
Bitmap: 一种存储像素的数据结构, 通过这个对象可得到一系列的图像属性. 可对图像进行旋转, 切割, 放大, 缩小等操作. 可将Bitmap渲染到Canvas获取新的Bitmap.
Bitmap
- Bitmap的创建
// 从源Bitmap获取immutable的Bitmap
createBitmap(Bitmap src);
// 从源Bitmap中(x, y)处指定width、height创建新immutable Bitmap
createBitmap(Bitmap src, int x, int y, int width, int height,);
// 返回经过matrix变换的immutable Bitmap.
createBitmap(Bitmap src, int x, int y, int width, int height, Matrix m, boolean filter);
// 返回具有指定width和height的mutable bitmap.
createBitmap(int width, int height, Config config);
// density:可以理解为相对屏幕密度, 1个DIP在160dpi的屏幕上大约为1像素大小. 以160dpi为基准线, density的值即为相对于160dpi屏幕的相对屏幕密度. 比如, 160dpi屏幕的density值为1, 320dpi屏幕的density值为2.
createBitmap(DisplayMetrics display, int width, int height, Config config);
// 若bitmap是ARGB_8888或RGBA_16F, hasAlpha可将bitmap标记为不透明.
createBitmap(int width, int height, Confit config, boolean hasAlpha);
// @param colorSpace: The color of the bitmap
createBitmap(int width, int height, Config config, boolean hasAlpha, ColorSpace colorSpace);
// mutable bitmap. initial density: display. color space: sRGB
createBitmap(DisplayMetrics display, int width, int height, Config config, boolean hasAlpha);
createBitmap(DisplayMetrics diaplay, int width, int height, Config config, boolean hasAlpha, ColorSpace colorSpace);
// immutable bitmap. 每个像素值设置为colors数组中的响应值. color space: sRGB
createBitmap(@NonNull @ColorInt int[] colors, int offset, int stride, int width, int height, Config config);
createBitmap(DisplayMetrics display, @ColorInt int[] colors, int offset, int stride, int width, int height, Config config);
createBitmap(int[] colors, int width, int height, Config config);
createBitmap(DisplayMetrics display, int colors[], int width, int height, Config config);
// immutable bitmap. 使用给定Picture资源创建Bitmap
createBitmap(Picture source);
createBitmap(Picture sourece, int width, int height, Config config);
- Bitmap压缩
/*
* 把当前bitmap压缩后写入输出流中, 若返回true, 则该bitmap可被BitmapFactory.decodeStream()恢复. 需要注意的是, 并不是所有的bitmap都支持所有的格式, BitmapFactory恢复回来的bitmap可能与原来有所不同.
* @param CompressFormat 枚举类型, 值有: JPEG/PNG/WEBP
* @param quality 对应0-100
* @param stream 压缩后结果的输出流
*/
public boolean compress(CompressFormat format, int quality, OutputStream steam) {
checkRecycled("Can't compress a recycled bitmap");
// do explicit check before calling the native method
if (stream == null) {
throw new NullPointerException();
}
if (quality < 0 || quality > 100) {
throw new IllegalArgumentException("quality must be 0..100");
}
Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "Bitmap.compress");
boolean result = nativeCompress(mNativePtr, format.nativeInt, quality, stream, new byte[WORKING_COMPRESS_STORAGE]);
Trace.traceEnd(Trace.TRACE_TAG_RESOURCES);
return result;
}
- Bitmap 回收
/*
* 释放与bitmap关联的native对象;
* 清除像素数据mBuffer的引用;
* mRecycled标志位设置为true后再调用bitmap方法则会异常;
*/
public void recycle() {
if (!mRecycled && mNativePtr != 0) {
if (nativeRecycle(mNativePtr)) {
// return value indicates whether native pixel object was actually recycled.
// false indicates that it is still in use at the native level and these
// objects should not be collected now. They will be collected later when the
// Bitmap itself is collected.
mNinePatchChunk = null;
}
mRecycled = true;
}
}
- Bitmap 所占内存
/*
* 返回存储bitmap对象所需要的内存, 当对bitmap所占内存区域进行服用的时候,该函数返回结果可能要大于getByteCount的值, 否则, 其与getByteCount的值相同.
* 此值在bitmap整个生命周期都不会改变
*/
public final int getAllocationByteCount() {
if (mRecycled) {
Log.w(TAG, "Called getAllocationByteCount() on a recycle()'d bitmap! "
+ "This is undefined behavior!");
return 0;
}
return nativeGetAllocationByteCount(mNativePtr);
}
/*
* 表示存储bitmap像素所需要的最小字节, 自从4.4之后, 这个就不能用来确定bitmap占用的内存了,需要用getAllocationByteCount.
*/
public final int getByteCount() {
if (mRecycled) {
Log.w(TAG, "Called getByteCount() on a recycle()'d bitmap! "
+ "This is undefined behavior!");
return 0;
}
// int result permits bitmaps up to 46,340 x 46,340
return getRowBytes() * getHeight();
}
BitmapFactory
-
获取bitmap方法
-
BitmapFactory.Options类
- Bitmap inBitmap
若给options设置了此bitmap, 那么通过options解码的时候, 返回的bitmap会尝试复用这个options中的bitmap, 如果不能复用返回null, 要求服用的bitmap是mutable; - boolean inJustDecodeBounds
若设为true, 那么解码方法返回null, 但是它会设置outXXX的值, 这样调用者可以在不用解码整张图片的前提下查询到bitmap的宽高; - int inSampleSize
对原来的图片进行采样, 如果inSampleSize为4, 那么图片的宽高会缩短到原来的1/4, 达到减小内存的目的; - Bitmap.Config inPreferredConfig
图片解码的格式要求.
- Bitmap inBitmap
https://www.jianshu.com/p/abc756158cf6
https://www.jianshu.com/p/5f02db4a225d