一、概述
项目中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); }