在 Android 应用中加载位图比较复杂,原因有很多种:
- 位图很容易就会耗尽应用的内存预算。例如,Pixel 手机上的相机拍摄的照片最大可达 4048x3036 像素(1200 万像素)。如果使用的位图配置为
[ARGB_8888](https://developer.android.com/reference/android/graphics/Bitmap.Config)
(Android 2.3(API 级别 9)及更高版本的默认设置),将单张照片加载到内存大约需要 48MB 内存(404830364 字节)。如此庞大的内存需求可能会立即耗尽该应用的所有可用内存。 -
在界面线程中加载位图会降低应用的性能,导致响应速度变慢,甚至会导致系统显示 ANR 消息。因此,在使用位图时,必须正确地管理线程处理。
单个像素的字节大小
单个像素的字节大小由Bitmap的一个可配置的参数Config来决定。 Bitmap中,存在一个枚举类Config,定义了Android中支持的Bitmap配置:
Bitmap加载方式
Bitmap 的加载方式有 Resource 资源加载、本地(SDcard)加载、网络加载等加载方式。
- 从本地(SDcard)文件读取
方式一
/**
- 获取缩放后的本地图片 *
- @param filePath 文件路径
- @param width 宽
- @param height 高 * @return
/
public static Bitmap readBitmapFromFile(String filePath, int width, int height) {
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
float srcWidth = options.outWidth;
float srcHeight = options.outHeight; int inSampleSize = 1;
if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height); } else {
inSampleSize = Math.round(srcWidth / width); }
}
options.inJustDecodeBounds = false; options.inSampleSize = inSampleSize;
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeFile(filePath, options);
}
方式二 (效率高于方式一)
/* - 获取缩放后的本地图片 *
- @param filePath 文件路径
- @param width 宽
- @param height 高 * @return
*/
public static Bitmap readBitmapFromFileDescriptor(String filePath, int
width, int height) {
try {
FileInputStream fis = new FileInputStream(filePath); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options); float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1;
if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height); } else {
inSampleSize = Math.round(srcWidth / width); }
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options);
} catch (Exception ex) {
}
return null;
}
- 从输入流中读取文件(网络加载)
/**
- 获取缩放后的本地图片 *
- @param ins 输入流
- @param width 宽
- @param height 高 * @return
/
public static Bitmap readBitmapFromInputStream(InputStream ins, int width,int height) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(ins, null, options); float srcWidth = options.outWidth;
float srcHeight = options.outHeight;
int inSampleSize = 1;
if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height); } else {
inSampleSize = Math.round(srcWidth / width); }
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeStream(ins, null, options);
}
3.Resource资源加载
Res资源加载方式:
public static Bitmap readBitmapFromResource(Resources resources, int
resourcesId, int width, int height) {
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(resources, resourcesId, options); float srcWidth = options.outWidth;
float srcHeight = options.outHeight; int inSampleSize = 1;
if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height); } else {
inSampleSize = Math.round(srcWidth / width); }
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeResource(resources, resourcesId, options);
}
此种方式相当的耗费内存 建议采用 decodeStream 代替 decodeResource 可以如下形式:
public static Bitmap readBitmapFromResource(Resources resources, int
resourcesId, int width, int height) {
InputStream ins = resources.openRawResource(resourcesId); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(ins, null, options);
float srcWidth = options.outWidth; float srcHeight = options.outHeight; int inSampleSize = 1;
if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height); } else {
inSampleSize = Math.round(srcWidth / width); }
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeStream(ins, null, options);
}
BitmapFactory.decodeResource 加载的图片可能会经过缩放,该缩放目前是放在 java 层做的,效率 比较低,而且需要消耗 java 层的内存。因此,如果大量使用该接口加载图片,容易导致OOM错误
BitmapFactory.decodeStream 不会对所加载的图片进行缩放,相比之下占用内存少,效率更高。 这两个接口各有用处,如果对性能要求较高,则应该使用 decodeStream ;如果对性能要求不高,且需 要 Android 自带的图片自适应缩放功能,则可以使用 decodeResource 。
4.Assets资源加载方式:
/* - 获取缩放后的本地图片
- @param filePath 文件路径,即文件名称 * @return
/
public static Bitmap readBitmapFromAssetsFile(Context context, String
filePath) {
Bitmap image = null;
AssetManager am = context.getResources().getAssets(); try {
InputStream is = am.open(filePath); image = BitmapFactory.decodeStream(is); is.close();
} catch (IOException e) { e.printStackTrace();
}
return image;
}
5.从二进制数据读取图片
public static Bitmap readBitmapFromByteArray(byte[] data, int width, int
height) {
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(data, 0, data.length, options); float srcWidth = options.outWidth;
float srcHeight = options.outHeight; int inSampleSize = 1;
if (srcHeight > height || srcWidth > width) {
if (srcWidth > srcHeight) {
inSampleSize = Math.round(srcHeight / height); } else {
inSampleSize = Math.round(srcWidth / width); }
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
Bitmap | Drawable | InputStream | Byte[ ] 之间进行转换
1.Drawable转化成Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight()); drawable.draw(canvas); return bitmap;
}
drawable 的获取方式: Drawable drawable=getResources().getDrawable(R.drawable.ic_launcher);
2.Bitmap转换成Drawable
public static Drawable bitmapToDrawable(Resources resources, Bitmap bm) {
Drawable drawable = new BitmapDrawable(resources, bm);
return drawable;
}
3.Bitmap转换成byte[ ]
public byte[] bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
4.byte[]转换成Bitmap
Bitmapbitmap=BitmapFactory.decodeByteArray(byte,0,b.length);
5.InputStream转换成Bitmap
InputStreamis=getResources().openRawResource(id); Bitmapbitmap=BitmaoFactory.decodeStream(is);
6.InputStream转换成byte[]
InputStream is = getResources().openRawResource(id);//也可以通过其他方式接收一个 InputStream对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[10242];
int len = 0;
while ((len = is.read(b, 0, b.length)) != -1) {
baos.write(b, 0, len);
baos.flush(); }
byte[] bytes = baos.toByteArray();
Bitmap常用操作
1.将Bitmap保存为本地文件:
public static void writeBitmapToFile(String filePath, Bitmap b, int quality)
{
try {
File desFile = new File(filePath);
FileOutputStream fos = new FileOutputStream(desFile); BufferedOutputStream bos = new BufferedOutputStream(fos); b.compress(Bitmap.CompressFormat.JPEG, quality, bos); bos.flush();
bos.close();
} catch (IOException e) { e.printStackTrace();
} }
2.图片压缩:
private static Bitmap compressImage(Bitmap image) {
if (image == null) {
return null;
}
ByteArrayOutputStream baos = null;
try {
baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] bytes = baos.toByteArray();
ByteArrayInputStream isBm = new ByteArrayInputStream(bytes); Bitmap bitmap = BitmapFactory.decodeStream(isBm);
return bitmap;
} catch (OutOfMemoryError e) {
} finally {
try {
if (baos != null) {
baos.close(); }
} catch (IOException e) {
} }
return null;
}
3.图片缩放:
/** - 根据scale生成一张图片
- @param bitmap
- @param scale 等比缩放值 * @return
/
public static Bitmap bitmapScale(Bitmap bitmap, float scale) {
Matrix matrix = new Matrix();
matrix.postScale(scale, scale); // 长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
return resizeBmp;
}
4.获取图片旋转角度:
/* - 读取照片exif信息中的旋转角度 *
- @param path 照片路径
-
@return角度
*/
private static int readPictureDegree(String path) { if (TextUtils.isEmpty(path)) {
return 0; }
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL); switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90: degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break; }
} catch (Exception e) {
}
return degree;
}
5.设置图片旋转角度
private static Bitmap rotateBitmap(Bitmap b, float rotateDegree) {
if (b == null) {
return null;
}
Matrix matrix = new Matrix(); matrix.postRotate(rotateDegree);
Bitmap rotaBitmap = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getHeight(), matrix, true); return rotaBitmap;
}
6.通过图片id获得Bitmap:
Bitmapbitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
7.通过 assest 获取 获得Drawable bitmap:
InputStream in = this.getAssets().open("ic_launcher");
Drawable da = Drawable.createFromStream(in, null);
Bitmap mm = BitmapFactory.decodeStream(in);
8.通过 sdcard 获得 bitmap
1 Bitmapbit=BitmapFactory.decodeFile("/sdcard/android.jpg");
9.view转Bitmap
publicstaticBitmapconvertViewToBitmap(Viewview,intbitmapWidth,int bitmapHeight){
Bitmap bitmap = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.ARGB_8888);
view.draw(new Canvas(bitmap));
return bitmap;
}
10.将控件转换为bitmap
public static Bitmap convertViewToBitMap(View view){ // 打开图像缓存
view.setDrawingCacheEnabled(true);
// 必须调用measure和layout方法才能成功保存可视组件的截图到png图像文件
// 测量View大小
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
// 发送位置和尺寸到View及其所有的子View
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); // 获得可视组件的截图
Bitmap bitmap = view.getDrawingCache();
return bitmap;
}
public static Bitmap getBitmapFromView(View view){
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(returnedBitmap); Drawable bgDrawable = view.getBackground(); if (bgDrawable != null)
bgDrawable.draw(canvas); else
canvas.drawColor(Color.WHITE); view.draw(canvas);
return returnedBitmap;
}
11.放大缩小图片
public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){ int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float)w / width);
float scaleHeight = ((float)h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,
true);
return newbmp;
}
12.获得圆角图片的方法
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap .getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
13.对 bitmap 进行裁剪
public Bitmap bitmapClip(Context context , int id , int x , int y){
Bitmap map = BitmapFactory.decodeResource(context.getResources(), id); map = Bitmap.createBitmap(map, x, y, 120, 120);
return map;
}
Bitmap内存模型
Bitmap的内存回收
- 在Android2.3.3之前推荐使用Bitmap.recycle()方法进行Bitmap的内存回收。
备注:只有当确定这个Bitmap不被引用的时候才能调用此方法,否则会有“Canvas: trying to use a recycled bitmap”这个错误。 - Android3.0之后
Android3.0之后,并没有强调Bitmap.recycle();而是强调Bitmap的复用
Save a bitmap for later use
使用LruCache对Bitmap进行缓存**,当再次使用到这个Bitmap的时候直接获取,而不用重走编码流程。
Use an existing bitmap
Android3.0(API 11之后)引入了BitmapFactory.Options.inBitmap字段,设置此字段之后解 码方法会尝试复用一张存在的Bitmap。这意味着Bitmap的内存被复用,避免了内存的回收 及申请过程,显然性能表现更佳。不过,使用这个字段有几点限制:
声明可被复用的Bitmap必须设置inMutable为true;
Android4.4(API 19)之前只有格式为jpg、png,同等宽高(要求苛刻),inSampleSize为1的 Bitmap才可以复用;
Android4.4(API 19)之前被复用的Bitmap的inPreferredConfig会覆盖待分配内存的Bitmap设 置的inPreferredConfig;
Android4.4(API 19)之后被复用的Bitmap的内存必须大于需要申请内存的Bitmap的内存; Android4.4(API 19)之前待加载Bitmap的Options.inSampleSize必须明确指定为1
获取Bitmap的大小 - getByteCount()
getByteCount()方法是在API12加入的,代表存储Bitmap的色素需要的最少内存。API19开始 getAllocationByteCount()方法代替了getByteCount()。 - getAllocationByteCount()
API19之后,Bitmap加了一个Api:getAllocationByteCount();代表在内存中为Bitmap分配的内存大
小。
public final int getAllocationByteCount() {
if (mBuffer == null) {
//mBuffer代表存储Bitmap像素数据的字节数组。
return getByteCount();
}
return mBuffer.length; } - getByteCount()与getAllocationByteCount()的区别
一般情况下两者是相等的
通过复用Bitmap来解码图片,如果被复用的Bitmap的内存比待分配内存的Bitmap大,那么 getByteCount()表示新解码图片占用内存的大小(并非实际内存大小,实际大小是复用的那个 Bitmap的大小),getAllocationByteCount()表示被复用Bitmap真实占用的内存大小(即mBuffer 的长度)
Bitmap占用内存大小计算
Bitmap作为位图,需要读入一张图片每一个像素点的数据,其主要占用内存的地方也正是这些像素数 据。对于像素数据总大小,我们可以猜想为:像素总数量 × 每个像素的字节大小,而像素总数量在矩形 屏幕表现下,应该是:横向像素数量 × 纵向像素数量,结合得到:
Bitmap内存占用 ≈ 像素数据总大小 = 横向像素数量 × 纵向像素数量 × 每个像素的字节大小
if (env->GetBooleanField(options, gOptions_scaledFieldID)) {
const int density = env->GetIntField(options, gOptions_densityFieldID);
const int targetDensity = env->GetIntField(options, gOptions_targetDensityFieldID);
const int screenDensity = env->GetIntField(options, gOptions_screenDensityFieldID);
if (density != 0 && targetDensity != 0 && density != screenDensity) {
scale = (float) targetDensity / density;
}
}
...
int scaledWidth = decoded->width();
int scaledHeight = decoded->height();
if (willScale && mode != SkImageDecoder::kDecodeBounds_Mode) {
scaledWidth = int(scaledWidth * scale + 0.5f);
scaledHeight = int(scaledHeight * scale + 0.5f);
}
...
if (willScale) {
const float sx = scaledWidth / float(decoded->width());
const float sy = scaledHeight / float(decoded->height());
bitmap->setConfig(decoded->getConfig(), scaledWidth, scaledHeight);
bitmap->allocPixels(&javaAllocator, NULL);
bitmap->eraseColor(0);
SkPaint paint;
paint.setFilterBitmap(true);
SkCanvas canvas(bitmap);
canvas.scale(sx, sy);
canvas.drawBitmap(decoded, 0.0f, 0.0f, &paint);
}
从上述代码中,我们看到bitmap最终通过canvas绘制出来,而canvas在绘制之前,有一个scale的操作,scale的值由
scale=(float)targetDensity/density;
这一行代码决定,即缩放的倍率和targetDensity和density相关,而这两个参数都是从传入的options中 获取到的
inDensity:Bitmap位图自身的密度、分辨率
inTargetDensity: Bitmap最终绘制的目标位置的分辨率
inScreenDensity: 设备屏幕分辨率
其中inDensity和图片存放的资源文件的目录有关,同一张图片放置在不同目录下会有不同的值:
可以验证几个结论:
- 图片放在drawable中,等同于放在drawable-mdpi中,原因为:drawable目录不具有屏幕密度特
性,所以采用基准值,即mdpi - 图片放在某个特定drawable中,比如drawable-hdpi,如果设备的屏幕密度高于当前drawable目
录所代表的密度,则图片会被放大,否则会被缩小 放大或缩小比例 = 设备屏幕密度 / drawable目录所代表的屏幕密度
因此,关于Bitmap占用内存大小的公式,从之前:
Bitmap内存占用 ≈ 像素数据总大小 = 横向像素数量 × 纵向像素数量 × 每个像素的字节大小
可以更细化为:
Bitmap内存占用 ≈ 像素数据总大小 = 图片宽 × 图片高× (设备分辨率/资源目录分辨率)^2 × 每个像 素的字节大小
高效加载大型位图,请参考官网
https://developer.android.com/topic/performance/graphics/load-bitmap
缓存位图,请参考官网
https://developer.android.com/topic/performance/graphics/cache-bitmap
管理位图,请参考官网
https://developer.android.com/topic/performance/graphics/manage-memory
本篇参考了https://www.jianshu.com/p/3f6f6e4f1c88