- Glide source version used in this article: 4.11.0
The last:Glide trilogy Gif loading principle
An appetizer
- My last article was reprinted by Guo Lin’s wechat public account, some friends want to know how Glide judges the picture type, today it came, with the source code parsing to come
The source code parsing
- So let’s start with the GifDrawable class and see where the GifDrawable is created
- Here we find a collection called ImageHeaderParser, which is literally a collection of image header parsers
- The method is called getType, the return type is ImageType, and this is the core code to get the ImageType
- Here we can see the rules for judging different image types. Let’s talk about how to judge JPG first
- I’m sure most of you are wondering, what does a 16-bit unsigned integer mean? Click on this link to tell you
- We all know that the integer is the basic data types, and any data on the computer in binary form, and in the smallest storage unit is a computer, each a 0 or 1, a word energy stored within eight binary data, in this case, the symbol is a minus sign, the signed integer first tag is positive or negative, Unsigned integers have no such flag bit.
- The value 0xFFD8 computed is 65496, which is a bit of a base conversion, since integers are written in decimal notation
-
These three lines of code are actually very simple, is to read the tag bit to determine the type of image
-
Now let’s see how the Gif type is determined. Okay
- In fact, we do not need to deduct this detail. It does not matter if we do not understand, we can directly judge by the name of the field
- Here we can draw a conclusion that JPG tag bit size is only two bytes, while GIF tag bit size is three bytes, so the question comes, why GIF tag bit size is three bytes?
- Compare the tag bits of the JPG we just talked about
- GIF markup bits are obviously larger than JPG, so JPG takes only two bytes to store, while GIF takes three bytes to fit
-
A little bit of basic computer knowledge is added here, isn’t that easier to understand?
-
Next, let’s have a quick look at what types of pictures Glide supports.
- That’s it, let’s summarize this piece of source code
conclusion
- Glide to judge the type of picture is actually very simple, read the first few bytes of the file stream, which represents the symbol bit of the picture type, and then judge the format of the picture by this symbol bit.