Android 一张图片(BitMap)占用内存的计算 图片内存优化
来源:互联网 发布:java写的程序怎么运行 编辑:程序博客网 时间:2024/05/16 01:03
在Android开发中,我现在发现很多人还不会对图片占用内存进行很好的计算。
因此撰写该博文来做介绍,期望达到抛砖引玉的作用。
Android中一张图片(BitMap)占用的内存主要和以下几个因数有关:图片长度,图片宽度,单位像素占用的字节数。 一张图片(BitMap)占用的内存=图片长度*图片宽度*单位像素占用的字节数 注:图片长度和图片宽度的单位是像素。 图片(BitMap)占用的内存应该和屏幕密度(Density)无关,虽然我暂时还拿不出直接证据。 创建一个BitMap时,其单位像素占用的字节数由其参数BitmapFactory.Options的inPreferredConfig变量决定。 inPreferredConfig为Bitmap.Config类型,Bitmap.Config类是个枚举类型,它可以为以下值This is very useful to efficiently store masks for instance. No color information is stored.
With this configuration, each pixel requires 1 byte of memory.
此时图片只有alpha值,没有RGB值,
一个像素占用一个字节Bitmap.Config
it is advised to use
ARGB_8888
instead. 这种格式的图片,看起来质量太差,已经不推荐使用。
Each pixel is stored on 2 bytes. The three RGB color channels and the alpha channel (translucency)
are stored with a 4 bits precision (16 possible values.)
This configuration is mostly useful if the application needs to store translucency information
but also needs to save memory. It is recommended to use ARGB_8888 instead of this configuration.
一个像素占用2个字节,alpha(A)值,Red(R)值,Green(G)值,Blue(B)值各占4个bites
共16bites,即2个字节Bitmap.Config
is stored with 8 bits of precision (256 possible values.) This configuration
is very flexible and offers the best quality. It should be used whenever possible
一个像素占用4个字节,alpha(A)值,Red(R)值,Green(G)值,Blue(B)值各占8个bites
共32bites,即4个字节
这是一种高质量的图片格式,电脑上普通采用的格式。它也是Android手机上一个BitMap的默认格式。Bitmap.Config
red is stored with 5 bits of precision (32 possible values),
green is stored with 6 bits of precision (64 possible values) and blue
is stored with 5 bits of precision. This configuration can produce slight visual
artifacts depending on the configuration of the source. For instance,
without dithering, the result might show a greenish tint.
To get better results dithering should be applied.
This configuration may be useful when using opaque bitmaps that do not require high color fidelity.
一个像素占用2个字节,没有alpha(A)值,即不支持透明和半透明,
Red(R)值占5个bites
对于没有透明和半透明颜色的图片来说,该格式的图片能够达到比较的呈现效果,
相对于ARGB_8888来说也能减少一半的内存开销。因此它是一个不错的选择。
另外我们通过android.content.res.Resources来取得一个张图片时,它也是以该格式来构建BitMap的
从Android4.0开始,该选项无效。即使设置为该值,系统任然会采用
透明度 红色 绿色 蓝色 简单点说
另外,需要注意这里的图片占用内存是指在Navtive中占用的内存,当然BitMap使用的绝大多数内存就是该内存。 因为我们可以简单的认为它就是BitMap所占用的内存。 Bitmap对象在不使用时,我们应该先调用recycle(),然后才它设置为null. 虽然Bitmap在被回收时可以通过BitmapFinalizer来回收内存。但是调用recycle()是一个良好的习惯 在Android4.0之前,Bitmap的内存是分配在Native堆中,调用recycle()可以立即释放Native内存。 从Android4.0开始,Bitmap的内存就是分配在dalvik堆中,即JAVA堆中的,调用recycle()并不能立即释放Native内存。但是调用recycle()也是一个良好的习惯。
通过dumpsys meminfo命令可以查看一个进程的内存使用情况, 当然也可以通过它来观察我们创建或销毁一张BitMap图片内存的变化,从而推断出图片占用内存的大小。 示例:adb shell
0 0
- Android 一张图片(BitMap)占用内存的计算 图片内存优化
- Android 一张图片(BitMap)占用内存的计算 图片内存优化
- Android 一张图片(BitMap)占用内存的计算 图片内存优化
- 计算程序中一张图片所占用的内存
- Android计算图片占用内存
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- Android中图片占用内存的计算
- iOS子线程更新UI的两种方法
- 对话框中添加状态栏
- 大数据测试类型&大数据测试步骤
- Android中使用level-list改变View显示状态
- EL表达式、基本语法、隐式对象,禁用EL表达式,EL主要作用
- Android 一张图片(BitMap)占用内存的计算 图片内存优化
- iOS音频
- 特征的转换_06-特征降维PCA
- 在git上push代码时缺少Change-Id
- GitHub for Windows使用教程(一)
- Counting the String 动态规划 + KMP
- Iterator和ListIterator主要区别有
- linux写定时任务
- 【Unity3D】图片纹理压缩方式,干货走起!