美文网首页
bitmap 图片压缩

bitmap 图片压缩

作者: 荞麦穗 | 来源:发表于2020-05-16 20:48 被阅读0次

应根据实际展示需要,压缩图片,而不是直接显示原图。手机屏幕比较小,
直接显示原图,并不会增加视觉上的收益,但是却会耗费大量宝贵的内存。

public static Bitmap decodeSampledBitmapFromResource(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);
    // 然后根据图片分辨率以及我们实际需要展示的大小,计算压缩率
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
     // 设置压缩率,并解码
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

使用完毕的图片,应该及时回收,释放宝贵的内存。

Bitmap bitmap = null; 
loadBitmapAsync(new OnResult(result){
      bitmap = result;
 });
...使用该 bitmap...
// 使用结束,在 2.3.3 及以下需要调用 recycle()函数,在 2.3.3 以上 GC 会自动管理,除非你明确不需要再用。
if (Build.VERSION.SDK_INT <= 10) { 
    bitmap.recycle();
}
bitmap = null;

使用 inBitmap 重复利用内存空间,避免重复开辟新内存。避免了一次内存的分配和回收,从而改善了运行效率
正例:

   public static Bitmap decodeSampledBitmapFromFile(String filename, int reqWidth, int reqHeight, ImageCache cache) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        BitmapFactory.decodeFile(filename, options);
        // 如果在 Honeycomb 或更新版本系统中运行,尝试使用 inBitmap
        if (Utils.hasHoneycomb()) {
            addInBitmapOptions(options, cache);
        }
        return BitmapFactory.decodeFile(filename, options);
    }

    private static void addInBitmapOptions(BitmapFactory.Options options, ImageCache cache) {
        // inBitmap 只处理可变的位图,所以强制返回可变的位图 
        options.inMutable = true;
        if (cache != null) {
            Bitmap inBitmap = cache.getBitmapFromReusableSet(options);
            if (inBitmap != null) {
            }
            options.inBitmap = inBitmap;
        }
    }

在使用inBitmap时注意:
1、inBitmap只能在3.0以后使用。2.3上,bitmap的数据是存储在native的内存区域,并不是在Dalvik的内存堆上。
2、在4.4之前,只能重用相同大小的bitmap的内存区域,而4.4之后你可以重用任何bitmap的内存区域,只要这块内存比将要分配内存的bitmap大就可以。

相关文章

  • 2019-11-05

    Bitmap Bitmap 细说Bitmap bitmap的六种压缩方式,Android图片压缩 1.先讲讲屏幕密...

  • 收集_Android源码文章

    一、Bitmap: Android bitmap压缩优化方案Android性能优化系列之Bitmap图片优化 二、...

  • Bitmap图片压缩

    Android中图片是以bitmap形式存在的,这篇文章主要介绍了Android实现图片压缩(bitmap的六种压...

  • Bitmap图片压缩

    图片压缩就是为了避免我们内存的溢出。而BitMap是android系统中对图像处理最重要的一个类,所以我们可以用他...

  • bitmap 图片压缩

    应根据实际展示需要,压缩图片,而不是直接显示原图。手机屏幕比较小,直接显示原图,并不会增加视觉上的收益,但是却会耗...

  • Volley图片压缩代码分析

    Volley的ImageRequest中的图片压缩代码 参考文章: Bitmap 解析 Bitmap 详解 你的 ...

  • 【Android开发基础系列】图片专题

    1 图片编辑处理 1.1 图片裁切 转载自:bitmap的六种压缩方式,Android图片压缩 http://bl...

  • Android

    Android常用图片压缩方式 质量压缩 尺寸压缩 1. 质量压缩 质量压缩通过相应算法进行优化Bitmap的位深...

  • [转]Bitmap的优化

    Bitmap的优化套路很简单,粗暴,就是让压缩。三种压缩方式: 对图片质量进行压缩 对图片尺寸进行压缩 使用lib...

  • bitmap的六种压缩方式,Android图片压缩

    此处分享一个图片的压缩处理方式 : bitmap的六种压缩方式,Android图片压缩 转载链接,点击查看详情 !

网友评论

      本文标题:bitmap 图片压缩

      本文链接:https://www.haomeiwen.com/subject/almzfftx.html