1.使用BitmapOptions 压缩图片
- //根据图片质量确定每个像素点所占字节数
- public static int getBytesPerPixel(Bitmap.Config config) {
- if (config == Bitmap.Config.ARGB_8888) {
- return 4;
- } else if (config == Bitmap.Config.RGB_565) {
- return 2;
- } else if (config == Bitmap.Config.ARGB_4444) {
- return 2;
- } else if (config == Bitmap.Config.ALPHA_8) {
- return 1;
- }
- return 1;
- }
-
- public static long getBitmapSizeInMemory(int imageW, int imageH) {
- return imageH * imageW * getBytesPerPixel(Bitmap.Config.ARGB_8888);
- }
- //获取的inSampleSize必须是2的倍数 ps:本函数(getScaleInSampleSize)只是一种参考,具体实现还需要根据实际情况有所动
- public static int getScaleInSampleSize(int resW, int resH, int desW, int desH) {
- int scaleW = resW / desW;
- int scaleH = resH / desH;
- int largeScale = scaleH > scaleW ? scaleH : scaleW;
- int sampleSize = 1;
- while (sampleSize < largeScale) {
- sampleSize *= 2;
- }
- return sampleSize;
- }
- //获取压缩图片
- public static Bitmap decodeBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
- {
- // 第一次解析将inJustDecodeBounds设置为true,来获取图片信息
- final BitmapFactory.Options options = new BitmapFactory.Options();
- options.inJustDecodeBounds = true;
- BitmapFactory.decodeResource(res, resId, options);
- int height = options.outHeight;
- int width = options.outWidth;
- String mimeType = options.outMimeType;
- // 调用上面定义的方法计算inSampleSize值
- options.inSampleSize = getScaleInSampleSize(width, height, reqWidth, reqHeight);
- //options.inPreferredConfig= Bitmap.Config.RGB_565; //如有必要,还可以修改图片质量,进一步减小图片大小
- // 使用获取到的inSampleSize值再次解析图片
- options.inJustDecodeBounds = false;
- return BitmapFactory.decodeResource(res, resId, options);
- }
2.使用LruCache:
- private LruCache<String, Bitmap> mMemoryCache;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // 获取到可用内存的最大值,使用内存超出这个值会引起OutOfMemory异常。
- // LruCache通过构造函数传入缓存值,以KB为单位。
- int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
- // 使用最大可用内存值的1/6作为缓存的大小。
- int cacheSize = maxMemory / 6;
- mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
- @Override
- protected int sizeOf(String key, Bitmap bitmap) {
- // 重写此方法来衡量每张图片的大小,默认返回图片数量。
- return bitmap.getByteCount() / 1024;
- }
- };
- }
- public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
- if (getBitmapFromMemCache(key) == null) {
- mMemoryCache.put(key, bitmap);
- }
- }
- public Bitmap getBitmapFromMemCache(String key) {
- return mMemoryCache.get(key);
- }
- public void loadBitmap(int resId, ImageView imageView) {
- final String imageKey = String.valueOf(resId);
- final Bitmap bitmap = getBitmapFromMemCache(imageKey);
- if (bitmap != null) {
- imageView.setImageBitmap(bitmap);
- } else {
- //如果缓存里面没有就使用默认的图片
- imageView.setImageResource(R.drawable.image_default);
- LoadWorkerTask task = new LoadWorkerTask();
- task.execute(resId);
- }
- }
- class LoadWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
- // 在后台加载图片。
- @Override
- protected Bitmap doInBackground(Integer... params) {
- final Bitmap bitmap = decodeBitmapFromResource(
- getResources(), params[0], 100, 100);
- addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
- return bitmap;
- }
- }