网上搜 到的一个图片缓存封装类

Home / Android MrLee 2016-8-31 2627

合理的应用图片缓存机制可以有效减少oom异常。
Bitmap进行缩放的工具类
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
/**
 * 压缩图片
 */
public class BitmapUtils {
    /**
     * 根据资源id获取到图片,并进行压缩
     * @param res
     * @param resId
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap decodeSampledBitmapFromResource(Resources res,
        int resId, int reqWidth, int reqHeight) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, opts);
        int inSampleSize = cacluateInSampleSize(opts, reqWidth, reqHeight);
        opts.inSampleSize = inSampleSize;
        opts.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeResource(res, resId, opts);
        return bitmap;
    }
    /**
     * 从byte数组中获取图片并压缩
     * @param data
     * @param reqWidth
     * @param reqHeight
     * @return
     */
    public static Bitmap decodeSampledBitmapFromByteArray(byte[] data,
        int reqWidth, int reqHeight) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, opts);
        int inSampleSize = cacluateInSampleSize(opts, reqWidth, reqHeight);
        opts.inJustDecodeBounds = false;
        opts.inSampleSize = inSampleSize;
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,
            opts);
        return bitmap;
    }
    private static int cacluateInSampleSize(BitmapFactory.Options opts,
        int reqWidth, int reqHeight) {
        if (opts == null)
            return 1;
        int inSampleSize = 1;
        int realWidth = opts.outWidth;
        int realHeight = opts.outHeight;
        if (realHeight > reqHeight || realWidth > reqWidth) {
            int heightRatio = realHeight / reqHeight;
            int widthRatio = realWidth / reqWidth;
            inSampleSize = (heightRatio > widthRatio) ? widthRatio :
                heightRatio;
        }
        return inSampleSize;
    }
}

使用lrucache对bitmap进行内存缓存的类
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.util.Log;
/**
 *
 *使用lrucache缓存图片到内存,做成了单例模式
 */
public class BitmapLruCacheHelper {
    private static final String TAG = null;
    private static BitmapLruCacheHelper instance = new BitmapLruCacheHelper();
    private LruCache < string, bitmap > cache = null;
    private BitmapLruCacheHelper() {
        int maxSize = (int)(Runtime.getRuntime().maxMemory() / 8);
        cache = new LruCache < string, bitmap = "" > (maxSize) {
            @Override
            protected int sizeOf(String key, Bitmap value) {
                return value.getRowBytes() * value.getHeight();
            }
        };
    }
    /**
     *加入缓存 
     * @param key
     * @param value
     */
    public void addBitmapToMemCache(String key, Bitmap value) {
        if (key == null || value == null) {
            return;
        }
        if (cache != null && getBitmapFromMemCache(key) == null) {
            cache.put(key, value);
            Log.i(TAG, "put to lrucache success");
        }
    }
    /**
     * 从缓存中获取图片
     * @param key
     * @return
     */
    public Bitmap getBitmapFromMemCache(String key) {
        if (key == null) {
            return null;
        }
        Bitmap bitmap = cache.get(key);
        Log.i(TAG, "from lrucache,bitmap=" + bitmap);
        return bitmap;
    }
    /**
     * 获取实例
     * @return
     */
    public static BitmapLruCacheHelper getInstance() {
        return instance;
    }
}

 

本文链接:https://www.it72.com/9987.htm

推荐阅读
最新回复 (0)
返回