Many people have provided solutions to display GIF images on Android. There are two main types. One is to enable the GIF decoder to decode the GIF when the program is running, and extract and display the frames in the GIF. Another method is to scatter GIF frames into n images using tools in advance, and switch and load the images in the program for display. Both methods can solve the problem of displaying some GIF images.
In practical applications, some very strange GIF images are incomplete, and these frames need to be merged and displayed on the basis of the previous frames. This may cause problems when such an image is extracted and displayed separately. When a bird flies in the forest, the effect becomes the first one to show the forest and then a bird in the blank background, if the height and coordinates of the frame where the bird is located are not properly processed, the displayed bird may be stretched to fill the imageview area.
The solution is actually very simple. frames must be consecutive rather than a single frame. In addition, the coordinates and width and height of frames must be extracted and applied to the display of frames.
After a GIF file is converted into a byte stream, 7 or 8 bytes indicate the GIF frame width, and 9 or 10 bytes indicate the height of the GIF frame. 1 and 2 bytes of the image data block represent the X coordinate offset, 3 and 4 bytes represent the Y coordinate offset, and 5, 6, and 7 and 8 represent the width and height of the current frame, respectively.
Byte [] bytes = stream. tobytearray ();
Int width = bytes [8] <8 | bytes [7]; // GIF box width
Int Height = bytes [10] <8 | bytes [9]; // GIF box height
When an image is displayed, the image is displayed in a stacked manner. When the image is drawn, the image is calculated in the drawing area of the container Based on the container rectangle, GIF rectangle, and frame rectangle information. In addition, remember that when the frame is converted to BMP, the color is argb, which can retain transparency and other effects.
This is because the company's project code is not publicly available for download.