首页 文章

如何在react-native android app中显示GIF?

提问于
浏览
21

我想在我的android react-native app中的我的Image标签中通过URL显示一个简单的gif,但是当我启动它时,没有显示图像 . docs中提供的代码仅适用于iOS,但不适用于Android:

<Image
  style={styles.gif}
  source={{uri: 'http://38.media.tumblr.com/9e9bd08c6e2d10561dd1fb4197df4c4e/tumblr_mfqekpMktw1rn90umo1_500.gif'}}
/>

这里有一个类似的问题但是已经说过这只适用于iOS:How do I display an animated gif in React Native?
关于这个提交它应该工作:https://github.com/facebook/react-native/commit/fcd7de5301655b39832d49908e5ca72ddaf91f7e

5 回答

  • 3

    我们通过使GIF支持可选等方式使核心库更小 .

    因为we have to manually opt-in for gif support in Android . 将以下两行添加到依赖项下的android / app / build.gradle文件中:

    compile "com.facebook.fresco:animated-gif:1.3.0"
    compile "com.facebook.fresco:animated-base-support:1.3.0"
    

    所以你的依赖项部分可能如下所示:

    dependencies {
      compile fileTree(dir: "libs", include: ["*.jar"])
      compile "com.android.support:appcompat-v7:23.0.1"
      compile "com.facebook.react:react-native:+"  // From node_modules
      compile "com.facebook.fresco:animated-gif:1.3.0"
      compile "com.facebook.fresco:animated-base-support:1.3.0"
    

    这解决了您的调试版本的问题,但如果您想在发布版本中解决它,那么您必须将以下行添加到您的proguard-rules文件中:

    -keep class com.facebook.imagepipeline.animated.factory.AnimatedFactoryImpl { public AnimatedFactoryImpl(com.facebook.imagepipeline.bitmaps.PlatformBitmapFactory, com.facebook.imagepipeline.core.ExecutorSupplier); }
    

    更多相关信息:https://github.com/facebook/fresco/issues/1177

    这已通过commit修复,并将包含在下一个版本中 .

  • 41

    以上所有内容对我来说都不适用于最新的React Native(v0.48) . 我必须在 android/app/build.gradle 中添加以下依赖项

    compile 'com.facebook.fresco:fresco:1.5.0' compile 'com.facebook.fresco:animated-gif:1.5.0'

  • 0

    您可以添加这些依赖项 . 我在版本(v0.44.0)中使用它:

    compile 'com.facebook.fresco:animated-base-support:0.14.1'
    compile 'com.facebook.fresco:animated-gif:0.14.1'
    

    在版本v0.50中,您只需要添加

    compile 'com.facebook.fresco:animated-gif:1.3.0'

  • 13

    我们升级到 "react-native": "^0.57.1" ,这阻止了我们的GIF动画播放;他们只是将动画的第一帧渲染为静态图像 .

    为了纠正这个问题,我们包括以下库:

    compile 'com.facebook.fresco:animated-gif:1.10.0'
    compile "com.facebook.fresco:animated-base-support:1.3.0"
    
    // (Only if supporting WebP.)
    compile 'com.facebook.fresco:animated-webp:1.10.0'
    compile 'com.facebook.fresco:webpsupport:1.10.0'
    
  • 15

    对于我添加如下 wasn't enough 的依赖项:

    compile 'com.facebook.fresco:animated-gif:1.9.0'
    

    我还必须在文件中升级我的gradle版本:

    android/gradle/wrapper/gradle-wrapper.properties 是这样的:

    distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
    

    以及文件中的构建工具版本:

    android/build.gradle 像这样:

    classpath 'com.android.tools.build:gradle:3.0.1'
    

相关问题