加载图片大小的计算
width * height * inTargetDensity/inDensity * inTargetDensity/inDensity * 一个像素所占的内存。
Android 默认图片类型:ARGB_8888(每个像素占用4byte内存)
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.p1,options);
Log.e(TAG, "width:" + options.outWidth + ":::height:" + options.outHeight);
Log.e(TAG, "bitmap:ByteCount = "+bitmap.getAllocationByteCount());
Log.e(TAG, "inDensity:" + options.inDensity + ":::inTargetDensity:" + options.inTargetDensity);
width:1311:::height:882
bitmap:ByteCount = 56664972
inDensity:160:::inTargetDensity:560
56664972/1311/882/4 =12.251.. 560/160 * 560/160 = 12.25
Android 图片类型
ALPHA_8:每个像素占用1byte内存。
ARGB_4444:每个像素占用2byte内存
ARGB_8888:每个像素占用4byte内存
RGB_565:每个像素占用2byte内存
Android 默认图片是ARGB_8888
Bitmap 占用内存
getByteCount()
getAllocationByteCount() 取代上面方法
对于webp的尝试
目前网络中图片仍然是占用流量较大的一部分,对于移动端更是如此,因此,如何在保证图片视觉不失真前提下缩小体积,对于节省带宽和电池电量十分重要。
WebP为网络图片提供了无损和有损压缩能力,同时在有损条件下支持透明通道。据官方实验显示:无损WebP相比PNG减少26%大小;有损WebP在相同的SSIM(Structural Similarity Index,结构相似性)下相比JPEG减少25%~34%的大小;有损WebP也支持透明通道,大小通常约为对应PNG的1/3。 同时,谷歌于2014年提出了动态WebP,拓展WebP使其支持动图能力。动态WebP相比GIF支持更丰富的色彩,并且也占用更小空间,更适应移动网络的动图播放。
作者:腾讯Bugly
链接:https://www.zhihu.com/question/27201061/answer/133992530
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String path = getApplicationContext().getFilesDir().getAbsolutePath();
byte[] bytes = compressBitmapToBytes(600,0,100, Bitmap.CompressFormat.JPEG);
Bitmap bp = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
saveBitmap(bp,path,"jpg");
saveBitmap(bp,path,"webp");
}
private Bitmap.CompressFormat compressFormat(String type){
switch (type){
case "jpg":
return Bitmap.CompressFormat.JPEG;
case "webp":
return Bitmap.CompressFormat.WEBP;
default:
return Bitmap.CompressFormat.JPEG;
}
}
public void saveBitmap(Bitmap bitmap,String path,String type){
File file = new File(path,"test");
if (!file.exists()) {
//通过file的mkdirs()方法创建目录中包含却不存在的文件夹
file.mkdirs();
}
File picture = new File(file+"/"+"1."+type);
FileOutputStream out = null;
try {
out = new FileOutputStream(picture);
long starten=System.currentTimeMillis(); //获取开始时间
bitmap.compress(compressFormat(type), 100,
out);
long enden=System.currentTimeMillis(); //获取结束时间
Log.e("编码运行时间: ",(enden-starten)+"ms");
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public byte[] compressBitmapToBytes(int reqWidth, int reqHeight, int quality, Bitmap.CompressFormat format) {
Bitmap bitmap = getSmallBitmap( reqWidth, reqHeight);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(format, quality, baos);
byte[] bytes = baos.toByteArray();
bitmap.recycle();
Log.i("compress", "Bitmap compressed success, size: " + bytes.length);
return bytes;
}
public Bitmap getSmallBitmap(int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bp = BitmapFactory.decodeResource(getResources(),R.drawable.p1,options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(getResources(),R.drawable.p1,options);
}
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
int h = options.outHeight;
int w = options.outWidth;
int inSampleSize = 0;
if (h > reqHeight || w > reqWidth) {
float ratioW = (float) w / reqWidth;
float ratioH = (float) h / reqHeight;
inSampleSize = (int) Math.min(ratioH, ratioW);
}
inSampleSize = Math.max(1, inSampleSize);
return inSampleSize;
}
}
webp 压缩能力确实强 但是在android 端 编码时间太长
添加方法
/***
* 计算ImageView中加载图片的具体尺寸和内存占用大小
* @param imageView
*/
private void calculateBitmapInfo(ImageView imageView) {
Drawable drawable = imageView.getDrawable();
if (drawable != null) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
Log.d(TAG, " bitmap width = " + bitmap.getWidth() + " bitmap height = " + bitmap.getHeight());
Log.d(TAG, " memory usage = " + bitmap.getAllocationByteCount());/**bitmap.getByteCount()方法不再使用*/
} else {
Log.d(TAG, "drawable is null!");
}
}