获取Bitmap大小
/**
* 得到bitmap的大小
*/
public static int getBitmapSize(Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //API 19
return bitmap.getAllocationByteCount();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
return bitmap.getByteCount();
}
// 在低版本中用一行的字节x高度
return bitmap.getRowBytes() * bitmap.getHeight(); //earlier version
}
Bitmap优化
一、主动释放Bitmap资源
if(bitmap != null && !bitmap.isRecycled()){
bitmap.recycle();
bitmap = null;
}
二、主动释放ImageView的图片资源
private static void recycleImageViewBitMap(ImageView imageView) {
if (imageView != null) {
BitmapDrawable bd = (BitmapDrawable) imageView.getDrawable();
rceycleBitmapDrawable(bd);
}
}
private static void rceycleBitmapDrawable(BitmapDrawable bitmapDrawable) {
if (bitmapDrawable != null) {
Bitmap bitmap = bitmapDrawable.getBitmap();
rceycleBitmap(bitmap);
}
bitmapDrawable = null;
}
private static void rceycleBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
}
三、主动释放ImageView的背景资源
public static void recycleBackgroundBitMap(ImageView view) {
if (view != null) {
BitmapDrawable bd = (BitmapDrawable) view.getBackground();
rceycleBitmapDrawable(bd);
}
}
private static void rceycleBitmapDrawable(BitmapDrawable bitmapDrawable) {
if (bitmapDrawable != null) {
Bitmap bitmap = bitmapDrawable.getBitmap();
rceycleBitmap(bitmap);
}
bitmapDrawable = null;
}
private static void rceycleBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
}
四、尽量少用Png图,多用NinePatch,WebP的图
五、使用大图之前,尽量先对其进行压缩
1.质量压缩
ByteArrayOutputStream baos = newByteArrayOutputStream();
intquality = Integer.valueOf(editText.getText().toString());
bit.compress(CompressFormat.JPEG, quality, baos);
byte[] bytes = baos.toByteArray();
bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Log.i("wechat", "压缩后图片的大小"+ (bm.getByteCount() / 1024/ 1024)
+ "M宽度为"+ bm.getWidth() + "高度为"+ bm.getHeight()
+ "bytes.length= "+ (bytes.length / 1024) + "KB"
+ "quality="+ quality);
其中quality是从edittext获取的数字,可以从0–100改变,这里出来的log是:
image.png
2.采样率压缩
BitmapFactory.Options options = newBitmapFactory.Options();
options.inSampleSize = 2;
bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/DCIM/Camera/test.jpg", options);
Log.i("wechat", "压缩后图片的大小"+ (bm.getByteCount() / 1024/ 1024)+ "M宽度为"+ bm.getWidth() + "高度为"+ bm.getHeight());
出来的log是
image.png3.缩放法压缩(martix)
Matrix matrix = newMatrix();
matrix.setScale(0.5f, 0.5f);
bm = Bitmap.createBitmap(bit, 0, 0, bit.getWidth(),bit.getHeight(), matrix, true);
Log.i("wechat", "压缩后图片的大小"+ (bm.getByteCount() / 1024/ 1024)
+ "M宽度为"+ bm.getWidth() + "高度为"+ bm.getHeight());
出来的log是
image.png
4.RGB_565法
BitmapFactory.Options options2 = newBitmapFactory.Options();
options2.inPreferredConfig = Bitmap.Config.RGB_565;
bm = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/DCIM/Camera/test.jpg", options2);
Log.i("wechat", "压缩后图片的大小"+ (bm.getByteCount() / 1024/ 1024)+ "M宽度为"+ bm.getWidth() + "高度为"+ bm.getHeight());
出来的log是:
image.png
5.createScaledBitmap
bm = Bitmap.createScaledBitmap(bit, 150, 150, true);
Log.i("wechat", "压缩后图片的大小"+ (bm.getByteCount() / 1024) + "KB宽度为"+ bm.getWidth() + "高度为"+ bm.getHeight());
出来的log是
image.png
网友评论