前言

有时会看到别人使用bitmap是用到ARGB_8888/RGB_565这类参数,那么这些参数是什么?对bitmap有什么影响?

Bitmap.Config

他们是Bitmap.Config的四种枚举类型。ARGB分别是alpha透明度和red、green、blue三色

  • ARGB_8888:分别用8位来记录4个值,所以每个像素会占用32位。
  • ARGB_4444:分别用4位来记录4个值,所以每个像素会占用16位。
  • RGB_565:分别用5位、6位和5位来记录RGB三色值,所以每个像素会占用16位。
  • ALPHA_8:根据注释应该是不保存颜色值,只保存透明度(8位),每个像素会占用8位。

内存大小

那么对bitmap有何影响?

这里都以ARGB_8888为基准来进行对比。

  • ARGB_4444:内存占用减少一半,但是每个值图片失真度很严重,这个参数本身已经不推荐使用了。
  • RGB_565:内存占用减少一半,舍弃了透明度,同时三色值也有部分损失,但是图片失真度很小。
  • ALPHA_8:内存占用减少3/4,没有颜色,只有透明度,即黑白。

测试代码

try {URL url = new URL("http://h.hiphotos.baidu.com/image/pic/item/b21c8701a18b87d6b025e513040828381f30fd53.jpg");HttpURLConnection connection = (HttpURLConnection)url.openConnection();InputStream in = connection.getInputStream();ByteArrayOutputStream content = new ByteArrayOutputStream();byte[] buffer = new byte[10 * 1024];int count;while((count = in.read(buffer)) > 0){content.write(buffer, 0, count);}byte[] data = content.toByteArray();BitmapFactory.Options options1 = new BitmapFactory.Options();options1.inPreferredConfig = Bitmap.Config.ARGB_8888;Bitmap bitmap1 = BitmapFactory.decodeByteArray(data, 0 , data.length, options1);//System.out.println("bitmap ARGB_8888 length " + bitmap1.getByteCount());System.out.println("bitmap ARGB_8888 length " + bitmap1.getRowBytes() * bitmap1.getHeight());BitmapFactory.Options options2 = new BitmapFactory.Options();options2.inPreferredConfig = Bitmap.Config.RGB_565;Bitmap bitmap2 = BitmapFactory.decodeByteArray(data, 0 , data.length, options2);//System.out.println("bitmap RGB_565 length " + bitmap2.getByteCount());System.out.println("bitmap RGB_565 length " + bitmap2.getRowBytes() * bitmap2.getHeight());BitmapFactory.Options options3 = new BitmapFactory.Options();options3.inPreferredConfig = Bitmap.Config.ALPHA_8;Bitmap bitmap3 = BitmapFactory.decodeByteArray(data, 0 , data.length, options3);//System.out.println("bitmap ALPHA_8 length " + bitmap3.getByteCount());System.out.println("bitmap ALPHA_8 length " + bitmap3.getRowBytes() * bitmap3.getHeight());BitmapFactory.Options options4 = new BitmapFactory.Options();options4.inPreferredConfig = Bitmap.Config.ARGB_4444;Bitmap bitmap4 = BitmapFactory.decodeByteArray(data, 0 , data.length, options4);//System.out.println("bitmap ARGB_4444 length " + bitmap4.getByteCount());System.out.println("bitmap ARGB_4444 length " + bitmap4.getRowBytes() * bitmap4.getHeight());} catch (Exception e) {e.printStackTrace();
}

但是实际测试中发现内存的大小与预期并不一样,那么是为什么?

ARGB_4444

首先,使用ARGB_4444发现内存没有变化,看看官方的解释

/** 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. Note: as of
Build.VERSION_CODES.KITKAT, any bitmap created with this configuration will be
created using ARGB_8888 instead.Deprecated
Because of the poor quality of this configuration, it is advised to use
ARGB_8888 instead. **/
@Deprecated
ARGB_4444   (4),

原来在KITKAT,即Android 19之后,这个参数就彻底无效了,只用这个参数会被默认替换为ARGB_8888,所以内存大小没有变化。

ALPHA_8

测试的时候,发现ALPHA_8也无效,不仅内存大小没变化,图片也还是原图,没有失去色彩。

我们来看看官方对inPreferredConfig的注释:

/* If this is non-null, the decoder will try to decode into this internal
configuration. If it is null, or the request cannot be met, the decoder will
try to pick the best matching config based on the system's screen depth, and
characteristics of the original image such as if it has per-pixel alpha
(requiring a config that also does). Image are loaded with the
Bitmap.Config.ARGB_8888 config by default. */
public Bitmap.Config inPreferredConfig = Bitmap.Config.ARGB_8888;

翻译过来就是如果inPreferredConfig不为null,那么解码器回尝试使用此参数指定的颜色模式来对图片进行解码,如果解码时发现不能满足这个模式,那么解码器回根据原图的特征及当前设备的屏幕位深,自动选择合适的颜色模式来解码。

简单说就是inPreferredConfig只是一个建议选项,最终使用的模式不一定是它指定的,这个要跟原图特征和当前设备的屏幕位深有关。

这就是当我么使用ALPHA_8无线的原因。另外根据上面的描述,RGB_565也可能无效,比如讲图片换成下面这个

https://pngimg.com/uploads/cocktail/cocktail_PNG51.png

总结

由于ARGB_4444已废除,而ALPHA_8需要在特殊条件下使用,一般用来做特殊需求的,所以我们大多数是用的还是ARGB_8888和RGB_565。

RGB_565能够在保证图片质量的情况下大大减少内存的开销,是解决oom的一种方法。但是一定要注意RGB_565是没有透明度的,如果图片本身需要保留透明度,那么就不能使用RGB_565。

同时也要注意,inPreferredConfig设置的模式并不一定会生效。

详解Bitmap之ARGB_8888/RGB_565/ALPHA_8/ARGB_4444相关推荐

  1. android bitmap 无损压缩,详解Bitmap尺寸压缩与质量压缩

    转载请注明出自flowsky37的博客,尊重他人辛苦劳动! 在Android系统中,关于图片处理的是一个既常见又比较棘手的问题.一个应用中,存在需要展示大量的图片的布局,那我们必须要小心翼翼的处理了, ...

  2. Bitmap详解(上)常用概念和常用API

    前言: 图片的操作我相信大家都操作过,在算法层面大家往往都是把图片转成MAT矩阵处理的,而Android 开发层面大多数都是bitmap位图操作.接下来我将分算法层面以及android层面来讲解一下图 ...

  3. Redis(十)——HyperLogLog 基数统计和 Bitmap位图场景详解

    文章目录 Redis(十)--HyperLogLog 基数统计和 Bitmap位图场景详解 1.HyperLogLog 基数统计 2.Bitmap位图场景详解 Redis(十)--HyperLogLo ...

  4. Redis 数据类型 Strings 类型详解

    Redis 数据类型 Strings 类型详解 bitmap 操作 SETBIT • SETBIT key offset value 起始版本:2.2.0 时间复杂度:O(1) 设置或者清空key的v ...

  5. 关于ARGB_8888、ALPHA_8、ARGB_4444、RGB_565的理解

    关于ARGB_8888.ALPHA_8.ARGB_4444.RGB_565的理解 A:透明度 R:红色 G:绿 B:蓝 Bitmap.Config ARGB_4444:每个像素占四位,即A=4,R=4 ...

  6. (4.6.31)Android Bitmap 详解

    文章目录 一.从相册加载一张图片 1.1 打开相册加载图片 1.2 根据Uri得到Bitmap 二.Bitmap 内存计算方式 2.1 density 和 densityDpi 2.2 getByte ...

  7. Android Bitmap详解

    一.Bitmap: Bitmap是Android系统中的图像处理的最重要类之一.用它可以获取图像文件信息,进行图像剪切.旋转.缩放等操作,并可以指定格式保存图像文件. 常用方法: public voi ...

  8. Android Bitmap(位图)详解

    一.背景 在Android开发中,任何一个APP都离不开图片的加载和显示问题.这里的图片来源分为三种:项目图片资源文件(一般为res/drawable目录下的图片文件).手机本地图片文件.网络图片资源 ...

  9. Bitmap精炼详解第(三)节:Bitmap的压缩

    一,前期基础知识储备 笔者之前有两篇文章:<Bitmap精炼详解第(一)节:Bitmap解析和加载><Bitmap精炼详解第(二)节:Bitmap常见处理方式>解释了一些Bit ...

最新文章

  1. 好书推荐:《零基础快速入行入职软件测试工程师》学测试一本就
  2. 记asp.net VB与C# 页面参数传值
  3. Ubuntu下添添加的用户没有sudo权限的解决办法
  4. wxWidgets:wxRichTextStyleListCtrl类用法
  5. 牛客网SQL篇刷题篇(24-31)
  6. Centos 5 手动安装yum
  7. 安装英伟达驱动,提示驱动和windows版本不兼容或是硬件不兼容的问题
  8. PHP工具箱配置和下载
  9. 计算机混合运算java,大话Java混合运算规则
  10. 计算机有网络却不能上网,电脑有网络,但是浏览器不能上网怎么办
  11. 产品经理的职业规划及绩效评估
  12. STM32名字含义以及其与ARM公司的关系
  13. 计算机安全知识策划书,安全知识竞赛策划书模板
  14. 发布自己的开源库到Cocoapods及部分常见错误
  15. 微电影的特征有哪些?
  16. 交换机-Smart Link AND Monitor Link的配置
  17. Windows下怎么修改iTunes备份路径?
  18. 嵌入式实时Hypervisor:XtratuM (2)
  19. AutoCAD支持Windows 7 64位?
  20. SAP-ML章<<<<第一节:物料账报错处理>>>>2021-06-10

热门文章

  1. 工作中的小技巧(一)
  2. [SQL Server]树形结构的创建
  3. 百度Google搜索框中,你不知道的变化
  4. Study Linux --- Shell Script
  5. winform app.cpnfig 文件的引用
  6. 【概率论】1-0:介绍
  7. cocos2d-x 消类游戏,类似Diamond dash 设计
  8. oracle 无法解析指定的连接标识符
  9. 大型主机CICS中间件基础
  10. 实现手电筒Flash Light 关键代码