学而实习之 不亦乐乎

Android 使用 Glide 频繁 setImageBitmap 引起 OOM 问题解决方法

2023-08-16 07:43:24
Glide.with(gsewmimg).load(getCodeBitmap(response.data.skip, R.mipmap.zhifuicon)).into(gsewmimg);

压缩前后。图片大小  2.22MB => 200KB

图片压缩方法:

Bitmap bitmap;
byte[] buff;
buff = Bitmap2Bytes(bitmap);
BitmapFactory.Options ontain = new BitmapFactory.Options();
ontain.inSampleSize = 7;//值1为原图。值越大,压缩图片越小1-20
bitmap = BitmapFactory.decodeByteArray(buff, 0, buff.length, ontain);

压缩方法2:

int height = (int) ( mybitmap.getHeight() * (512.0 / mybitmap.getWidth()) );
mybitmap = Bitmap.createScaledBitmap(mybitmap, 512, height, true);