用Glide加载Gif导致的卡顿,说一下你的优化思路,安卓移动开发课程设计

本文分析了Glide加载Gif导致卡顿的原因,通过详细解读加载流程,提出通过在native层解码GIF和使用FrameSequenceDrawable的双缓冲机制来优化性能,降低内存消耗和CPU使用率,从而改善Gif动画的流畅性。
摘要由CSDN通过智能技术生成

// 将像素数据转换为图像

return setPixels(currentFrame, previousFrame);

}

//…

private Bitmap setPixels(GifFrame currentFrame, GifFrame previousFrame) {

// Final location of blended pixels.

// 存储上一帧的Bitmap像素数据

final int[] dest = mainScratch;

// clear all pixels when meet first frame and drop prev image from last loop

if (previousFrame == null) {

if (previousImage != null) {

// 回收上一帧的Bitmap

bitmapProvider.release(previousImage);

}

previousImage = null;

// 并且将Bitmap的像素填充黑色

Arrays.fill(dest, COLOR_TRANSPARENT_BLACK);

}

if (previousFrame != null && previousFrame.dispose == DISPOSAL_PREVIOUS

&& previousImage == null) {

//上一帧数据为被废弃了,清空

Arrays.fill(dest, COLOR_TRANSPARENT_BLACK);

}

// fill in starting image contents based on last image’s dispose code

//1. 将上一帧的 数据注入到dest数组中

if (previousFrame != null && previousFrame.dispose > DISPOSAL_UNSPECIFIED) {

if (previousFrame.dispose == DISPOSAL_BACKGROUND) {

// Start with a canvas filled with the background color

@ColorInt int c = COLOR_TRANSPARENT_BLACK;

if (!currentFrame.transparency) {

c = header.bgColor;

if (currentFrame.lct != null && header.bgIndex == currentFrame.transIndex) {

c = COLOR_TRANSPARENT_BLACK;

}

} else if (framePointer == 0) {

isFirstFrameTransparent = true;

}

// The area used by the graphic must be restored to the background color.

int downsampledIH = previousFrame.ih / sampleSize;

int downsampledIY = previousFrame.iy / sampleSize;

int downsampledIW = previousFrame.iw / sampleSize;

int downsampledIX = previousFrame.ix / sampleSize;

int topLeft = downsampledIY * downsampledWidth + downsampledIX;

int bottomLeft = topLeft + downsampledIH * downsampledWidth;

for (int left = topLeft; left < bottomLeft; left += downsampledWidth) {

int right = left + downsampledIW;

for (int pointer = left; pointer < right; pointer++) {

dest[pointer] = c;

}

}

} else if (previousFrame.dispose == DISPOSAL_PREVIOUS && previousImage != null) {

// Start with the previous frame

// 获取上一帧的Bitmap中的数据,并且将数据更新到dest中.

previousImage.getPixels(dest, 0, downsampledWidth, 0, 0, downsampledWidth,

downsampledHeight);

}

}

// Decode pixels for this frame into the global pixels[] scratch.

// 2. 解析当前帧的数据到dest中

decodeBitmapData(currentFrame);

if (currentFrame.interlace || sampleSize != 1) {

copyCopyIntoScratchRobust(currentFrame)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值