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

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

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

Bitmap 构造方法很多:

Bitmap.Config 说明 ALPHA_8 ARGB_4444  ARGB_8888 RGB_565

几个都带有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 来定义常量同样可以实现逻辑,为什么搞个枚举出来

  速度更快:

  1 作者:ccloomi
  2 链接:https://www.zhihu.com/question/48915384/answer/151489315
  3 来源:知乎
  4 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  5 
  6 事实上使用枚举类型的性能几乎是使用静态类的16倍,至于为什么我们看代码,这里我写了两个类,Age类是一个枚举类型public enum Age {
  7     _12,_16,_19
  8 }
  9 AG类是一个静态类public class AG {
 10     public static final int _12=12;
 11     public static final int _16=16;
 12     public static final int _19=19;
 13 }
 14 然后是一个测试Main类public class Main {
 15     public void age(Age ag){
 16         switch (ag) {
 17         case _12:
 18             break;
 19         case _16:
 20             break;
 21         case _19:
 22             break;
 23         default:
 24             break;
 25         }
 26     }
 27     public void age(int ag){
 28         switch (ag) {
 29         case 12:
 30             break;
 31         case 16:
 32             break;
 33         case 19:
 34             break;
 35         default:
 36             break;
 37         }
 38     }
 39     public static void main(String[] args) {
 40         long t=0,limit=1000000;
 41         Main mm=new Main();
 42         
 43         t=System.currentTimeMillis();
 44         for(long i=0;i<limit;i++){
 45             mm.age(Age._16);
 46         }
 47         System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");
 48         
 49 
 50         t=System.currentTimeMillis();
 51         for(long i=0;i<limit;i++){
 52             mm.age(16);
 53         }
 54         System.out.println("耗时"+(System.currentTimeMillis()-t)+"ms");
 55     }
 56 }
 57 同样的方法我们执行100万此,执行结果耗时0ms
 58 耗时16ms
 59 使用枚举类的方法秒杀了静态类,至于为什么我们可以看Main这个方法编译后生成的字节码 // access flags 0x1
 60   public age(Lcom/test/en/Age;)V
 61    L0
 62     LINENUMBER 11 L0
 63     INVOKESTATIC com/test/en/Main.$SWITCH_TABLE$com$test$en$Age ()[I
 64     ALOAD 1
 65     INVOKEVIRTUAL com/test/en/Age.ordinal ()I
 66     IALOAD
 67     TABLESWITCH
 68       1: L1
 69       2: L2
 70       3: L3
 71       default: L4
 72    L1
 73     LINENUMBER 13 L1
 74    FRAME SAME
 75     GOTO L4
 76    L2
 77     LINENUMBER 15 L2
 78    FRAME SAME
 79     GOTO L4
 80    L3
 81     LINENUMBER 17 L3
 82    FRAME SAME
 83     GOTO L4
 84    L4
 85     LINENUMBER 21 L4
 86    FRAME SAME
 87     RETURN
 88    L5
 89     LOCALVARIABLE this Lcom/test/en/Main; L0 L5 0
 90     LOCALVARIABLE ag Lcom/test/en/Age; L0 L5 1
 91     MAXSTACK = 2
 92     MAXLOCALS = 2
 93 
 94   // access flags 0x1
 95   public age(I)V
 96    L0
 97     LINENUMBER 23 L0
 98     ILOAD 1
 99     LOOKUPSWITCH
100       12: L1
101       16: L2
102       19: L3
103       default: L4
104    L1
105     LINENUMBER 25 L1
106    FRAME SAME
107     GOTO L4
108    L2
109     LINENUMBER 27 L2
110    FRAME SAME
111     GOTO L4
112    L3
113     LINENUMBER 29 L3
114    FRAME SAME
115     GOTO L4
116    L4
117     LINENUMBER 33 L4
118    FRAME SAME
119     RETURN
120    L5
121     LOCALVARIABLE this Lcom/test/en/Main; L0 L5 0
122     LOCALVARIABLE ag I L0 L5 1
123     MAXSTACK = 1
124     MAXLOCALS = 2
125 注意看使用枚举类的方法使用的是TABLESWITCH
126 使用静态类的方法使用的确是LOOKUPSWITCH
127 这两个性能差距是很大的
View Code

相关文章:

  • 2022-12-23
  • 2021-05-22
  • 2021-12-25
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2021-07-05
  • 2022-12-23
  • 2022-03-07
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案