Android开发——优化Glide加载超大gif图

文章介绍了GifLibDrawableResource类的封装,用于处理GifDrawable的销毁和内存占用计算,以及DrawableBytesTranscoder和GifLibEncoder的使用。着重讨论了如何在Glide框架中替换原生Gif支持并兼容GifLib库,但面临兼容性问题和Transformation限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值