Android使用Glide加载Gif慢 获取gif时间
1,解决Glide加载Gif非常慢问题
1 | Glide.with(MainActivity. this ).load(url).asGif().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imageView); |
为其添加缓存策略,其中缓存策略可以为:Source及None,None及为不缓存,Source缓存原型.如果为ALL和Result就不行
2,加载第一贞:
1 | Glide.with(context).load(gifUrl).asBitmap().into(imageViewGifAsBitmap); |
3,控制动画次数:
1 | Glide.with( this ).load(getResource()).diskCacheStrategy(DiskCacheStrategy.SOURCE).into( new GlideDrawableImageViewTarget(imageView, 1 )); |
4,GIF 时间:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | Glide.with(FirstActivity. this ) .load(file) .asGif() .fitCenter() .diskCacheStrategy(DiskCacheStrategy.SOURCE) .listener( new RequestListener<File, GifDrawable>() { @Override public boolean onException(Exception e, File model, Target<GifDrawable> target, boolean isFirstResource) { return false ; } @Override public boolean onResourceReady( final GifDrawable resource, File model, Target<GifDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) { new Thread( new Runnable() { @Override public void run() { int duration = 0 ; try { GifDrawable gifDrawable = (GifDrawable) resource; GifDecoder decoder = gifDrawable.getDecoder(); for ( int i = 0 ; i < gifDrawable.getFrameCount(); i++) { duration += decoder.getDelay(i); } mGifAdTime = duration; } catch (Throwable e) { } } }).start(); return false ; } }) .into(mAdImg); |
4.+ 版本获取gif时间
Glide.with(FirstActivity.this) .asGif() .load(file) .fitCenter() .diskCacheStrategy(DiskCacheStrategy.DATA) .listener(new RequestListener<GifDrawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) { return false; } @Override public boolean onResourceReady(final GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) { new Thread(new Runnable() { @Override public void run() { // 计算动画时长 int duration = 0; try { GifDrawable gifDrawable = (GifDrawable) resource; //设置循环播放次数为1次 gifDrawable.setLoopCount(1); //GifDecoder decoder = gifDrawable.getDecoder();//4.0开始没有这个方法了 Drawable.ConstantState state = gifDrawable.getConstantState(); if (state != null) { //不能混淆GifFrameLoader和GifState类 Object gifFrameLoader = GlideGifUtil.getValue(state, "frameLoader"); if (gifFrameLoader != null) { Object decoder = GlideGifUtil.getValue(gifFrameLoader, "gifDecoder"); if (decoder != null && decoder instanceof GifDecoder) { for (int i = 0; i < gifDrawable.getFrameCount(); i++) { duration += ((GifDecoder) decoder).getDelay(i); } } } } } catch (Throwable e) { } } }).start(); return false; } }) .into(mAdImg);
public class GlideGifUtil { /** * 通过字段名从对象或对象的父类中得到字段的值 * * @param object 对象实例 * @param fieldName 字段名 * @return 字段对应的值 * @throws Exception */ public static Object getValue(Object object, String fieldName) throws Exception { if (object == null) { return null; } if (TextUtils.isEmpty(fieldName)) { return null; } Field field = null; Class<?> clazz = object.getClass(); for (; clazz != Object.class; clazz = clazz.getSuperclass()) { try { field = clazz.getDeclaredField(fieldName); field.setAccessible(true); return field.get(object); } catch (Exception e) { //这里甚么都不要做!并且这里的异常必须这样写,不能抛出去。 //如果这里的异常打印或者往外抛,则就不会执行clazz = clazz.getSuperclass(),最后就不会进入到父类中了 } } return null; } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)