用Glide加载Gif导致的卡顿,说一下你的优化思路,安卓手机内存优化

本文深入探讨了Glide在加载Gif时的解码过程,包括StreamGifDecoder和ByteBufferGifDecoder的角色,以及如何通过优化采样率、配置Bitmap.Config来提升性能。同时,分析了GifDrawable如何播放GIF动画,并指出GifFrameLoader在动画调度中的作用。针对加载Gif导致的卡顿问题,提出了优化思路。
摘要由CSDN通过智能技术生成

1)首先来介绍一下Gif相关的解码器


Glide的构造中可以找到Gif的相关信息.

Glide(

@NonNull Context context,

//) {

//…

List imageHeaderParsers = registry.getImageHeaderParsers();

//…

GifDrawableBytesTranscoder gifDrawableBytesTranscoder = new GifDrawableBytesTranscoder();

//…

registry

//…

/* GIFs */

.append(

Registry.BUCKET_GIF,

InputStream.class,

GifDrawable.class,

new StreamGifDecoder(imageHeaderParsers, byteBufferGifDecoder, arrayPool))

.append(Registry.BUCKET_GIF, ByteBuffer.class, GifDrawable.class, byteBufferGifDecoder)

.append(GifDrawable.class, new GifDrawableEncoder())

/* GIF Frames */

// Compilation with Gradle requires the type to be specified for UnitModelLoader here.

.append(

GifDecoder.class, GifDecoder.class, UnitModelLoader.Factory.getInstance())

.append(

Registry.BUCKET_BITMAP,

GifDecoder.class,

Bitmap.class,

new GifFrameResourceDecoder(bitmapPool))

//…

.register(GifDrawable.class, byte[].class, gifDrawableBytesTranscoder);

ImageViewTargetFactory imageViewTargetFactory = new ImageViewTargetFactory();

//…

}

因此第一步可以发现Glide是通过创建StreamGifDecoder来解码Gif的InputStream流.

public class StreamGifDecoder implements ResourceDecoder<InputStream, GifDrawable> {

@Override

public Resource decode(@NonNull InputStream source, int width, int height,

@NonNull Options options) throws IOException {

// 1. 用一个byte数组来接收InputStream流

byte[] data = inputStreamToBytes(source);

if (data == null) {

return null;

}

// 2.使用ByteBuffer包装处理原始的数据流,

//思考为什么用ByteBuffer呢?

/**

@link StandardGifDecoder#setData();

// Initialize the raw data buffer.

rawData = buffer.asReadOnlyBuffer();

rawData.position(0);

rawData.order(ByteOrder.LITTLE_ENDIAN); // 小端对齐.从低位到高位排序

*/

ByteBuffer byteBuffer = ByteBuffer.wrap(data);

return byteBufferDecoder.decode(byteBuffer, width, height, options);

}

}

具体细节如下:

  • 使用byte[] 数组接收InputStream

  • 然后在通过处理之后的byte[]交给ByteBufferGifDecoder进行下一阶段的处理工作(完善对InputStream的解码工作);

public class ByteBufferGifDecoder implements ResourceDecoder<ByteBuffer, GifDrawable> {

//…

@Override

public GifDrawableResource decode(@NonNull ByteBuffer source, int width, int height,

@NonNull Options options) {

final GifHeaderParser parser = parserPool.obtain(source);

try {

return decode(source, width, height, parser, options);

} finally {

parserPool.release(parser);

}

}

@Nullable

private GifDrawableResource decode(

ByteBuffer byteBuffer, int width, int height, GifHeaderParser parser, Options options) {

long startTime = LogTime.getLogTime();

try {

// 1.获取GIF头部信息

final GifHeader header = parser.parseHeader();

if (header.getNumFrames() <=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值