GifLibDrawableResource 封装GifDrawable提供销毁和内存占用大小计算(用于lrucache)
DrawableBytesTranscoder和GifLibBytesTranscoder 用于转换
GifLibEncoder 用于序列化成文件
重要的解析类:
class GifLibByteBufferDecoder …
@Throws(IOException::class)
override fun handles(source: ByteBuffer, options: Options): Boolean {
//必须要 开启anim
val isAnim = !options.get(GifOptions.DISABLE_ANIMATION)!!
//根据文件头判断是否是gif
val isGif = ImageHeaderParserUtils.getType(parsers, source) == ImageType.GIF
// DES: 此日志主要关注 gif图并且 设置了不允许动画的地方
if (isGif) Log.e(TAG, “gif options anim ->$isAnim”)
return isAnim && isGif
}
/*解析方法/
private fun decode(byteBuffer: ByteBuffer, width: Int, height: Int, parser: GifHeaderParser, options: Options): GifLibDrawableResource? {
val startTime = LogTime.getLogTime()
return try {
val header = parser.parseHeader()
if (header.numFrames <= 0 || header.status != GifDecoder.STATUS_OK) {
// If we couldn’t decode the GIF, we will end up with a frame count of 0.
return null
}
//进行采样设置
val sampleSize = getSampleSize(header, width, height)
//创建解析器构建模式
val builder = GifDrawableBuilder()
builder.from(byteBuffer)
builder.sampleSize(sampleSize)
builder.isRenderingTriggeredOnDraw = true
// pl.droidsonroids.gif.GifOptions gifOptions = new pl.droidsonroids.gif.GifOptions();
// DES: 不含透明层可以加速渲染 但是透明的gif会渲染黑色背景
// gifOptions.setInIsOpaque();
val gifDrawable = builder.build()
val loopCount = gifDrawable.loopCount
if (loopCount <= 1) {
//循环一次的则矫正为无限循环
Log.v(TAG, “Decoded GIF LOOP COUNT WARN $loopCount”)
gifDrawable.loopCount = 0
}
Gif