如何丝滑般地加载超大gif图?
共 2294字,需浏览 5分钟
·
2021-11-18 07:55
作者: forJrking
https://juejin.cn/user/2612095355987191
Why
为何要优化glide的gif support呢?要回到2年前,我们需要在页面支持很多png或者gif的图作为活动氛围的背景,而运营商给的gif图都很大(>5mb),就会出现内存抖动致APP卡顿,还有gif会掉帧,虽然通过gif压缩可以减小体积,但是显示效果会大打折扣。调研加载支持gif的图片加载库,也只有glide还有Fresco了。而项目已经有glide了,那么我们需要的就是去做优化了。(额外说下Fresco还支持webp动图)
那么放在现在,glide本身对gif的支持优化已经很多了,之前多个gif同时渲染的内存抖动问题已经没了,掉帧问题也有优化但是还是存在。但是内存占用还有cpu占用率却还是比优化的版本差,今天就来分享下如何优化。
优化前后
一加1手机android6.0,加载6张2-5mb的gif图。
How
要优化首先要了解gif的特性,glide如何渲染gif的。由于源码的剖析过程非常长,都可以单独出个文章了。这里只说下要点:
gif特性
gif文件的文件头前3个字节必然为'G''I''F' gif中的每一帧图片尺寸相同 gif中每帧会有间隔时间
ImageHeaderParserUtils.getType(..)检测资源是否为gif com.bumptech.glide.load.resource.gif.GifDrawable为最终渲染gif的drawable StreamGifDecoder和ByteBufferGifDecoder把流转换为GifDrawable GifDrawableEncoder把GifDrawable转换为File 以上组件模块在com.bumptech.glide.Glide的构造方法内进行注册组装,而且支持注册自己的组件
优化的技术选型
融合glide
GifLibDecoder 解析io InputStream 实际是获取byte[]交给下面的解析器
GifLibByteBufferDecoder 解析 byte[]生成 GifDrawable的 包装 GifLibDrawableResource
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
}
GifLibDrawableResource(gifDrawable, byteBuffer)
} catch (e: IOException) {
Log.v(TAG, "Decoded GIF Error" + e.message)
null
} finally {
Log.v(TAG, "Decoded GIF from stream in " + LogTime.getElapsedMillis(startTime))
}
}
}
class GifLibEncoder : ResourceEncoder<GifDrawable?> {
override fun getEncodeStrategy(options: Options): EncodeStrategy {
return EncodeStrategy.SOURCE
}
override fun encode(data: Resource, file: File, options: Options) : Boolean {
var success = false
if (data is GifLibDrawableResource) {
val byteBuffer = data.buffer
try {
ByteBufferUtil.toFile(byteBuffer, file)
success = true
} catch (e: IOException) {
e.printStackTrace()
}
// DES: 将 resource 编码成文件
Log.d(TAG, "GifLibEncoder -> $success -> ${file.absolutePath}")
}
return success
}
}
通过Registry注册组件
append(..)追加到最后,当内部的组件在 handles()返回false或失败时候使用追加组件 prepend(..)追加到前面,当你的组件在失败时候使用原生提供组件 replace(..)替换组件 register(..)注册组件
@JvmStatic
fun registerGifLib(glide: Glide, registry: Registry) {
//优先使用gifLib-Gif
val bufferDecoder = GifLibByteBufferDecoder(registry.imageHeaderParsers)
val gifLibTranscoder = GifLibBytesTranscoder()
val bitmapBytesTranscoder = BitmapBytesTranscoder()
val gifTranscoder = GifDrawableBytesTranscoder()
registry.prepend(
Registry.BUCKET_GIF, java.io.InputStream::class.java, GifDrawable::class.java,
GifLibDecoder(registry.imageHeaderParsers, bufferDecoder, glide.arrayPool)
).prepend(
Registry.BUCKET_GIF,
java.nio.ByteBuffer::class.java,
GifDrawable::class.java, bufferDecoder
).prepend(
GifDrawable::class.java, GifLibEncoder()
).register(
Drawable::class.java, ByteArray::class.java,
DrawableBytesTranscoder(
glide.bitmapPool,
bitmapBytesTranscoder,
gifTranscoder,
gifLibTranscoder
)
).register(
GifDrawable::class.java, ByteArray::class.java, gifLibTranscoder
)
}
验证组件是否注册成功
IGlide.with(view).load(url)
.placeholder(R.color.colorAccent)
.listener(object : RequestListener{
override fun onResourceReady(
resource: Drawable?, model: Any?,
target: Target?, dataSource: DataSource?, isFirstResource: Boolean) :
Boolean {
if (resource is pl.droidsonroids.gif.GifDrawable) {
Log.d("TAG", "giflib的 Gifdrawable")
} else if (resource is com.bumptech.glide.load.resource.gif.GifDrawable) {
Log.d("TAG", "glide的 Gifdrawable")
}
return false
}
override fun onLoadFailed(e: GlideException?, model: Any?,target: Target?, isFirstResource: Boolean) : Boolean = false
}).into(view)
log: com.example.mydemo D/TAG: giflib的 Gifdrawable
transform缺陷
class BaseRequestOptions...
@NonNull
T transform(@NonNull Transformation<Bitmap> transformation, boolean isRequired) {
...省略
DrawableTransformation drawableTransformation =
new DrawableTransformation(transformation, isRequired);
transform(Bitmap.class, transformation, isRequired);
transform(Drawable.class, drawableTransformation, isRequired);
transform(BitmapDrawable.class, drawableTransformation.asBitmapDrawable(), isRequired);
//对gifdrawble的 Transformation 支持缘由
transform(GifDrawable.class, new GifDrawableTransformation(transformation), isRequired);
return selfOrThrowIfLocked();
}
}
class GifLibDrawableTransformation(wrapped: Transformation<Bitmap>) : Transformation<GifDrawable> {
private val wrapped: Transformation= Preconditions.checkNotNull(wrapped)
override fun transform(
context: Context, resource: Resource, outWidth: Int, outHeight: Int : Resource
){
val drawable = resource.get()
drawable.transform = object : Transform {
private val mDstRectF = RectF()
override fun onBoundsChange(rct: Rect) = mDstRectF.set(rct)
override fun onDraw(canvas: Canvas, paint: Paint, bitmap: Bitmap) {
val bitmapPool = Glide.get(context).bitmapPool
val bitmapResource: Resource= BitmapResource(bitmap, bitmapPool)
val transformed = wrapped.transform(context, bitmapResource, outWidth, outHeight)
val transformedFrame = transformed.get()
canvas.drawBitmap(transformedFrame, null, mDstRectF, paint)
}
}
return resource
}
...
}
//每次调用 transform 时候注入下
val circleCrop = CircleCrop()
IGlideModule.with(this)
.load("http://tva2.sinaimg.cn/large/005CjUdnly1g6lwmq0fijg30rs0zu4qp.gif")
.transform(GifDrawable::class.java, GifLibDrawableTransformation(circleCrop))
.transform(circleCrop)
.into(iv_2)
总结
https://github.com/forJrking/GlideGifLib
• 耗时2年,Android进阶三部曲第三部《Android进阶指北》出版!
• 『BATcoder』做了多年安卓还没编译过源码?一个视频带你玩转!
• 重生!进阶三部曲第一部《Android进阶之光》第2版 出版!
BATcoder技术群,让一部分人先进大厂
大家好,我是刘望舒,腾讯TVP,著有三本业内知名畅销书,连续四年蝉联电子工业出版社年度优秀作者,百度百科收录的资深技术专家。
想要加入 BATcoder技术群,公号回复
BAT
即可。
为了防止失联,欢迎关注我的小号
微信改了推送机制,真爱请星标本公号👇