解决Glide+RecyclerView gif动画加载后不播放问题
一、概述
项目中RecyclerView+Glide加载网络的gif动画,但是列表中红gif动画在华为手机上并不显示(鸿蒙os),尝试了很多方法,oppo和小米均正常。最后测试下来是传入的Context有问题。把Context换成Activity或者Application
后动画自动执行了。
ps:这可能是个特例,还没找到为甚这样写的原因,如果有知道的希望能够告知。
二、代码实例
主要是下面Glide.with(Application/Activity),这样写能够加载成功,如果直接写context则加载不成功。或者写view.getContext()也加载不成功。
public void loadNetGif(Context context,String gifPath, ImageView view) { Glide.with(context).asGif().load(gifPath).listener(new RequestListener<GifDrawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) { view.postDelayed(() -> { Log.e("TAG","莎莎哈啊"); }, 1); return false; } @Override public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) { try { Field gifStateField = GifDrawable.class.getDeclaredField("state"); gifStateField.setAccessible(true); Class gifStateClass = Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable$GifState"); Field gifFrameLoaderField = gifStateClass.getDeclaredField("frameLoader"); gifFrameLoaderField.setAccessible(true); Class gifFrameLoaderClass = Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader"); Field gifDecoderField = gifFrameLoaderClass.getDeclaredField("gifDecoder"); gifDecoderField.setAccessible(true); Class gifDecoderClass = Class.forName("com.bumptech.glide.gifdecoder.GifDecoder"); Object gifDecoder = gifDecoderField.get(gifFrameLoaderField.get(gifStateField.get(resource))); Method getDelayMethod = gifDecoderClass.getDeclaredMethod("getDelay", int.class); getDelayMethod.setAccessible(true); //设置只播放一次 resource.setLoopCount(-1); //获得总帧数 int count = resource.getFrameCount(); int delay = 0; for (int i = 0; i < count; i++) { //计算每一帧所需要的时间进行累加 delay += (int) getDelayMethod.invoke(gifDecoder, i); } view.postDelayed(() -> { }, delay); } catch (NoSuchFieldException e) { }catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return false; } }).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.ALL).into(view); }
分类:
Android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2014-09-22 android ImageLoader加载本地图片的工具类
2014-09-22 android 给LinearLayout中添加一定数量的控件,并让着一定数量的控件从右到左移动,每隔若干秒停顿一下,最后一个view链接第一个view,然后继续移动循环往复,形成一个死循环简单动画效果
2014-09-22 学习及资讯类网站