这篇文章的目的是了解Bitmap.Config

你可以在使用这个方法的时候会遇到

Bitmap android.graphics.Bitmap.createBitmap(int width, int height, Config config)

需要一个Bitmap 然后在上面画一些东西。

Bitmap 构造方法很多:

几个都带有Config这个参数。这个参数倒底是什么?

看一下源码:

 /**
* Possible bitmap configurations. A bitmap configuration describes
* how pixels are stored. This affects the quality (color depth) as
* well as the ability to display transparent/translucent colors.
*/
public enum Config {
// these native values must match up with the enum in SkBitmap.h /**
* Each pixel is stored as a single translucency (alpha) channel.
* 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_8 (1), /**
* Each pixel is stored on 2 bytes and only the RGB channels are
* encoded: 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.
*/
RGB_565 (3), /**
* 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 {@link #ARGB_8888} instead of this
* configuration.
*
* Note: as of {@link android.os.Build.VERSION_CODES#KITKAT},
* any bitmap created with this configuration will be created
* using {@link #ARGB_8888} instead.
*
* @deprecated Because of the poor quality of this configuration,
* it is advised to use {@link #ARGB_8888} instead.
*/
@Deprecated
ARGB_4444 (4), /**
* Each pixel is stored on 4 bytes. Each channel (RGB and alpha
* for translucency) 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.
*/
ARGB_8888 (5); final int nativeInt; private static Config sConfigs[] = {
null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888
}; Config(int ni) {
this.nativeInt = ni;
} static Config nativeToConfig(int ni) {
return sConfigs[ni];
}
}

第一眼看上去应是个类类型,上来new一个吧。但是不行。

点进去看是枚举类型。

再仔细看一下源码:

    public enum Config {

        ALPHA_8     (1),
@Deprecated
ARGB_4444 (4),
ARGB_8888 (5); final int nativeInt; private static Config sConfigs[] = {
null, ALPHA_8, null, RGB_565, ARGB_4444, ARGB_8888
}; Config(int ni) {
this.nativeInt = ni;
}
static Config nativeToConfig(int ni) {
return sConfigs[ni];
}
}

有点蒙逼吧。这语言平见也少用。其实枚举类型类似于常量,直接用就行。如下:

Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_4444);

好吧,枚举这东西用得少,可以复习一下。

https://www.cnblogs.com/happyPawpaw/archive/2013/04/09/3009553.html

顺便多说两句,为什么要用枚举。

枚举在java1.5以后才推荐使用。

1.switch

JDK1.6之前的switch语句只支持int,char,enum类型,使用枚举,能让我们的代码可读性更强。

2.使用static final int 来定义常量同样可以实现逻辑,为什么搞个枚举出来

速度更快:

 作者:ccloomi
链接:https://www.zhihu.com/question/48915384/answer/151489315
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 事实上使用枚举类型的性能几乎是使用静态类的16倍,至于为什么我们看代码,这里我写了两个类,Age类是一个枚举类型public enum Age {
_12,_16,_19
}
AG类是一个静态类public class AG {
public static final int _12=12;
public static final int _16=16;
public static final int _19=19;
}
然后是一个测试Main类public class Main {
public void age(Age ag){
switch (ag) {
case _12:
break;
case _16:
break;
case _19:
break;
default:
break;
}
}
public void age(int ag){
switch (ag) {
case 12:
break;
case 16:
break;
case 19:
break;
default:
break;
}
}
public static void main(String[] args) {
long t=0,limit=1000000;
Main mm=new Main(); t=System.currentTimeMillis();
for(long i=0;i<limit;i++){
mm.age(Age._16);
}
System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms"); t=System.currentTimeMillis();
for(long i=0;i<limit;i++){
mm.age(16);
}
System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");
}
}
同样的方法我们执行100万此,执行结果耗时0ms
耗时16ms
使用枚举类的方法秒杀了静态类,至于为什么我们可以看Main这个方法编译后生成的字节码 // access flags 0x1
public age(Lcom/test/en/Age;)V
L0
LINENUMBER 11 L0
INVOKESTATIC com/test/en/Main.$SWITCH_TABLE$com$test$en$Age ()[I
ALOAD 1
INVOKEVIRTUAL com/test/en/Age.ordinal ()I
IALOAD
TABLESWITCH
1: L1
2: L2
3: L3
default: L4
L1
LINENUMBER 13 L1
FRAME SAME
GOTO L4
L2
LINENUMBER 15 L2
FRAME SAME
GOTO L4
L3
LINENUMBER 17 L3
FRAME SAME
GOTO L4
L4
LINENUMBER 21 L4
FRAME SAME
RETURN
L5
LOCALVARIABLE this Lcom/test/en/Main; L0 L5 0
LOCALVARIABLE ag Lcom/test/en/Age; L0 L5 1
MAXSTACK = 2
MAXLOCALS = 2 // access flags 0x1
public age(I)V
L0
LINENUMBER 23 L0
ILOAD 1
LOOKUPSWITCH
12: L1
16: L2
19: L3
default: L4
L1
LINENUMBER 25 L1
FRAME SAME
GOTO L4
L2
LINENUMBER 27 L2
FRAME SAME
GOTO L4
L3
LINENUMBER 29 L3
FRAME SAME
GOTO L4
L4
LINENUMBER 33 L4
FRAME SAME
RETURN
L5
LOCALVARIABLE this Lcom/test/en/Main; L0 L5 0
LOCALVARIABLE ag I L0 L5 1
MAXSTACK = 1
MAXLOCALS = 2
注意看使用枚举类的方法使用的是TABLESWITCH
使用静态类的方法使用的确是LOOKUPSWITCH
这两个性能差距是很大的

通过注解,来约束一些变量及参数。使的程序在编写之时就变得健壮。

回到主题:

看看枚举变量倒底什么含义。
public static final Bitmap.Config  ALPHA_8 
public static final Bitmap.Config  ARGB_4444  
public static final Bitmap.Config  ARGB_8888
public static final Bitmap.Config  RGB_565

ALPHA_8 

源码注释很清楚:
/**
* Each pixel is stored as a single translucency (alpha) channel.
* 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 通道值,就是透明值。
Each pixel is stored as a single translucency (alpha) channel
对处理遮罩很有效,
No color information is stored.
没有存颜色值就是说没有rgb信息。 (此处画重点)
each pixel requires 1 byte of memory
每个pixel占一个byte.1个字节。
所以网上很多博文瞎扯的。还是自己看源码靠谱。
再看下RGB_565 
/**
* Each pixel is stored on 2 bytes and only the RGB channels are
* encoded: 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.
*/
每个像素2byte存储空间。
红:5位(5 bits)对应32色。就是有32种不同的红色。
绿:green is stored with 6 bits of precision (64 possible)绿 6 位 64色。
蓝:5位 32色 自然没有alpha值。看明白了没有透明值。自己去建一个没有透明bitmap试一下吧,看看什么效果。
最后使用在do not require high color fidelity (图片质量要求不高的场景)

能表示多少种颜色呢 32*64*32 =65536 色。是不是很熟啊。

再看下
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 {@link #ARGB_8888} instead of this
* configuration.
*
* Note: as of {@link android.os.Build.VERSION_CODES#KITKAT},
* any bitmap created with this configuration will be created
* using {@link #ARGB_8888} instead.
*
* @deprecated Because of the poor quality of this configuration,
* it is advised to use {@link #ARGB_8888} instead.
*/
不再多说了,很好理解,共2byte 16位 。4位透明,R,G,B各占四位。
ARGB_8888   

顾名思义:A8位,G,R,B各8位。

32位真彩色是什么意思呢就是这个类型来表示,一共能表示多少种色彩呢
2^8*2^8*2^8 = 16777216 ;
这里所说的所有东西都是针对一个像素来讨论的。
那么一张图,在内存当中占多少内存呢,
内存=宽*高*每个像素点占的存。
以argb_8888为例:
100*100像素的图片在内存中占:100*100*32 bit 0.305M
看看满屏一张图占多少,1920*1080*32=63.28M
是不是很恐怖呢63M!!!

Bitmap.Config 说明 ALPHA_8 ARGB_4444 ARGB_8888 RGB_565的更多相关文章

  1. Android开发之Bitmap.Config.RGB_565

    在学习xutils框架的时候,看到sample代码中有一行这样的代码: bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565); Bi ...

  2. 关于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 ...

  3. Bitmap.Config 详解

    前言 Android是一个内存相当吃紧的系统,那么在做程序的过程中使用内存就需要相当谨慎,而我们接触最大的大对象估计就是Bitmap了,那么下面就根据Bitmap.Config值的介绍来看下Bitma ...

  4. 图片处理之-Bitmap.Config,jpeg压缩与大小

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

  5. android bitmap压缩几种色彩详解

    android中的大图片一般都要经过压缩才显示,不然容易发生oom,一般我们压缩的时候都只关注其尺寸方面的大小,其实除了尺寸之外,影响一个图片占用空间的还有其色彩细节. 打开Android.graph ...

  6. 8.4.4 Picasso

    Picasso 收到加载及显示图片的任务,创建 Request 并将它交给 Dispatcher,Dispatcher 分发任务到具体 RequestHandler,任务通过 MemoryCache ...

  7. [置顶] Android开发百科全书

    友情提示根据目录 快速查找问题 %1$s %1$d Android string 1.整型,比如"我今年23岁了",这个23是整型的.在string.xml中可以这样写,<s ...

  8. 解决android 大图OOM的两种方法

    最近做程序中,需要用到一张大图.这张图片是2880*2180大小的,在我开发所用的华为3C手机上显示没有问题,但是给米3装的时候,一打开马上报OOM错误.给nexus5装,则是图片无法出来,DDMS中 ...

  9. Android图片缓存之Bitmap详解

    前言: 最近准备研究一下图片缓存框架,基于这个想法觉得还是先了解有关图片缓存的基础知识,今天重点学习一下Bitmap.BitmapFactory这两个类. 图片缓存相关博客地址: Android图片缓 ...

随机推荐

  1. jscodeshift 简易教程

    本文首发于 https://github.com/whxaxes/blog/issues/10 背景 jscodeshift 是 fb 出的一个 codemod toolkit,基于 recast 这 ...

  2. OpenWRT添加模块 Makefile和Config.in

    添加模块编译 在网上找了一下,很多关于编译Openwrt系统的资料,不过这些事情芯片厂商提供的开发包都已经办得妥妥了,但是没有找到系统介绍的资料,添加一个包的介绍有不多,其中有两个很有参考价值: ht ...

  3. (10.23)Java小知识!

    ---恢复内容开始--- 方法的定义: 一般情况下,定义一个方法包含以下语法: 修饰符 返回值类型 方法名 (参数类型 参数名 , ...){ ... 方法体 ... return 返回值; } 修饰 ...

  4. VS2017生成解决方案报错,提示对路径的访问被拒绝

    目前我用的vs2017的版本是15.3.5.生成解决方案有时会提示如下: 开始以为是权限的问题,找到相应的目录设置everyone权限,再次生成还是不行.重启VS试了下,还是不行. 最后无奈重启下电脑 ...

  5. 典型的NIO代码

    public void selector() throws IOException { ByteBuffer buffer = ByteBuffer.allocate(1024); Selector ...

  6. 自学LinkedBlockingQueue源码

    自学LinkedBlockingQueue源码 参考:http://www.jianshu.com/p/cc2281b1a6bc 本文需要关注的地方 生产者-消费者模式好处: 读取和插入操作所使用的锁 ...

  7. 关于docker使用的几个小问题(一)

    由于刚接触docker踩了几个坑,希望本文对网瘾少年有所帮助. Docker分CE版(社区版)和EE版(商用版),具体安装流程参考文档介绍,在此不再赘述.下面分Windows和Linux分别踩坑: 一 ...

  8. mybatis 分页问题 (个人认为算是个bug)

    问题描述:相同的查寻条件, 分页显示的结果和.net版本的分页结果数量一样,排序不一样, 不同的页有相同的数据.比如:第2面和第3页都有同一条相同的数据. 核心代码: //自己实现 int total ...

  9. 基于winsocket的框体Server和Client

    前面学了一点Winsock的知识,会编写简单的Server和Client,现在就想通过VS2008编写框体的Server和Client,而不是在控制台上的操作了,毕竟学编程就是要多加练习,在实践中发现 ...

  10. Windows Message Queue

    Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...