一个bitmap工具类

本文探讨了Android中Bitmap的使用,包括如何高效地加载、处理和存储位图,以避免内存溢出并提升应用性能。同时,分享了一个实用的Bitmap工具类,包含了常见优化策略和方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class BitmapUtil {

    private static volatile BitmapUtil instance;

    private BitmapUtil() {
    }

    public static BitmapUtil getInstance() {
        if (instance == null) {
            synchronized (BitmapUtil.class) {
                if (instance == null) {
                    instance = new BitmapUtil();
                }
            }
        }
        return instance;
    }

    /**
     * decodeFile4.4以上直接使用的FileInputStream,需要多次读取
     * decodeStream可以使用BufferedInputStream提高读写性能
     *
     * @param is io流
     * @param isRGB  是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmap(InputStream is, boolean isRGB) {
        if (isRGB) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.RGB_565;
            return BitmapFactory.decodeStream(new BufferedInputStream(is),null,options);
        }
        return BitmapFactory.decodeStream(new BufferedInputStream(is));
    }

    /**
     * bitmap缩略图
     * @param is io流
     * @param maxWidth 最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @param isRGB 是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmap(InputStream is,int maxWidth, int maxHeight, boolean isRGB) {
        byte[] bytes = input2Byte(is);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
        options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
        if (isRGB) {
            options.inPreferredConfig = Bitmap.Config.RGB_565;
        }
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    }

    /**
     * 读取资源文件夹下图片
     *
     * @param res getResources
     * @param id  文件id
     * @param isRGB  是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapResources(Resources res, int id, boolean isRGB) {
        TypedValue value = new TypedValue();
        InputStream is = res.openRawResource(id, value);
        return getBitmap(is,isRGB);
    }

    /**
     * 读取资源文件夹下图片的缩略图
     *
     * @param res       getResources
     * @param id        文件id
     * @param maxWidth  最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @param isRGB     是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapResources(Resources res, int id, int maxWidth, int maxHeight, boolean isRGB) {
        TypedValue value = new TypedValue();
        InputStream is = res.openRawResource(id, value);
        return getBitmap(is,maxWidth,maxHeight,isRGB);
    }

    /**
     * 获取ByteArrayOutputStream字节数组
     *
     * @param is InputStream
     * @return 字节数组
     */
    private byte[] input2Byte(InputStream is) {
        if (is == null) return null;
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = is.read(b, 0, 1024)) != -1) {
                os.write(b, 0, len);
            }
            return os.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 读取sd卡图片
     *
     * @param path     图片文件路径
     * @param fileName 文件名字
     * @return 高清bitmap
     */
    public Bitmap getBitmapFile(String path, String fileName, boolean isRGB) {
        return getBitmapFile(new File(path, fileName),isRGB);
    }

    /**
     * 读取sd卡图片
     *
     * @param file 图片文件
     * @param isRGB 是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapFile(File file, boolean isRGB) {
        try {
            return getBitmap(new FileInputStream(file),isRGB);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读取sd卡图片的缩略图
     *
     * @param path      图片文件路径
     * @param fileName  文件名字
     * @param maxWidth  最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @param isRGB     是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapFile(String path, String fileName, int maxWidth, int maxHeight, boolean isRGB) {
        return getBitmapFile(new File(path, fileName), maxWidth, maxHeight, isRGB);
    }

    /**
     * 读取sd卡图片的缩略图
     *
     * @param file      图片文件
     * @param maxWidth  最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @param isRGB     是否使用RGB_565压缩图片
     * @return bitmap
     */
    public Bitmap getBitmapFile(File file, int maxWidth, int maxHeight, boolean isRGB) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), options);
        options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
        if (isRGB) {
            options.inPreferredConfig = Bitmap.Config.RGB_565;
        }
        options.inJustDecodeBounds = false;
        try {
            return BitmapFactory.decodeStream(new BufferedInputStream(new FileInputStream(file)), null, options);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取缩放比例
     *
     * @param options   BitmapFactory.Options
     * @param maxWidth  最大宽度 建议为屏幕宽度
     * @param maxHeight 最大高度 建议为屏幕宽度
     * @return inSampleSize
     */
    private int calculateInSampleSize(BitmapFactory.Options options, int maxWidth, int maxHeight) {
        int height = options.outHeight;
        int width = options.outWidth;
        int inSampleSize = 1;
        while (height > maxHeight || width > maxWidth) {
            height >>= 1;
            width >>= 1;
            inSampleSize <<= 1;
        }
        return inSampleSize;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值