您的位置:首页 > 其它

Glide 使用问题一:加载gif过慢

2016-12-24 11:41 337 查看
摘要: 最近项目中用到glide来加载gif图片,发现加载过慢,偶尔还会遇到Bad position (limit 4234016): 4234042这样错误。

最近项目中遇到需要展示gif图片,而且能对其进行放缩功能,所以有用到TouchImageView,这里https://github.com/MikeOrtiz/TouchImageView源码可以学习研究一下。

好吧, 直接贴下问题代码:

Glide.with(mContext)
.load(imageUrl)
.fitCenter()
.error(errorResourceId)
.override(mScreenWidth, mScreenHeight)
.into(new GlideDrawableImageViewTarget(TouchImageView) {
@Override
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);
}

@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
}
});

通过这种方式去加载网络图片(jpg或者gif),发现1m左右的gif加载可能几分钟都不会onResourceReady,原因就在于用了fitCenter()这个方法,目的是让图片撑满屏幕,结果glide在加载过程中,对gif的每一帧做fitCenter处理(TransformationUtils.fitCenter()),而gif可能存在一两百帧的情况,导致大部分时间用在了这里。

那问题找到了,去掉fitCenter就好了吧;结果不行,加载出来的图片就是原尺寸了,存在不能撑满屏幕的情况,有一点不明白,我加了.override(mScreenWidth, mScreenHeight),传的屏幕尺寸,图片还是显示很小的一块。

那既要加载迅速,又需要加载的图片撑满屏幕,怎么办呢?这里的解决方案就是加载原尺寸图片,在onResourceReady对touchImageView做放缩,直接贴代码.

Glide.with(mContext)
.load(imageUrl)
.error(errorResourceId)
.override(mScreenWidth, mScreenHeight)
.into(new GlideDrawableImageViewTarget(imageView) {
@Override
public void onResourceReady(GlideDrawable drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);
float scaleX = (float) mScreenWidth / (float) drawable.getIntrinsicWidth();
float scaleY = (float) mScreenHeight / (float) drawable.getIntrinsicHeight();
float scale = Math.min(scaleX, scaleY);
scale = scale < 1 ? 1 : scale;
imageView.setZoom(scale);
}

@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
super.onLoadFailed(e, errorDrawable);
}
});

好了,解决问题,glide的源码还需要继续研究^_^
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gif 加载慢