【问题标题】:How do I display an animated gif in React Native?如何在 React Native 中显示动画 gif?
【发布时间】:2016-06-06 07:23:21
【问题描述】:

如何在 react native 中显示动画 gif。这是我尝试过的。

<Image source={{uri: "loading"}} />

它适用于 .png 文件,但当我使用 .gif 文件时,它是空白的。我在某处读到尝试将.gif 重命名为.png,但这仅显示动画 gif 的一帧而没有动画。有什么想法吗?

【问题讨论】:

    标签: react-native


    【解决方案1】:

    对于 RN

    默认情况下,android react native 应用程序不支持 Gif 图像。 您需要设置使用 Fresco 来显示 gif 图像。 代码:

    编辑您的android/app/build.gradle 文件并添加以下代码:

    dependencies: { 
    
        ...
    
        compile 'com.facebook.fresco:fresco:1.+'
    
        // For animated GIF support
        compile 'com.facebook.fresco:animated-gif:1.+'
    
        // For WebP support, including animated WebP
        compile 'com.facebook.fresco:animated-webp:1.+'
        compile 'com.facebook.fresco:webpsupport:1.+' 
    }
    

    那么你需要重新打包app,你可以通过两种方式显示gif图像。

    1-> <Image 
            source={require('./../images/load.gif')}  
            style={{width: 100, height: 100 }}
        />
    
    2-> <Image 
            source={{uri: 'http://www.clicktorelease.com/code/gif/1.gif'}}  
            style={{width: 100, height:100 }} 
        />
    

    对于 RN >= 0.60

    implementation 'com.facebook.fresco:animated-gif:1.12.0' //instead of
    
    implementation 'com.facebook.fresco:animated-gif:2.0.0'   //use
    

    希望对其他人有所帮助,

    【讨论】:

    • 对于动画 GIF,animated-base-support 对我不起作用(RN 0.41.0),但 animated-gif(WebP 下的第一个)起作用。这就是我最终添加到我的 build.gradle 文件中的内容:compile "com.facebook.fresco:animated-gif:1.1.0"
    • @RyanH。你有animated-base-support animated-gif吗?我想知道animated-base-support 是否是基本要求,而animated-gif 只是错误地与webp 分组。
    • @cedmans 我只需要animated-gif。见stackoverflow.com/q/38169519/305383
    • 现在 compile 已被替换为 implementation
    【解决方案2】:

    您需要以这种方式添加扩展并要求它:

    <Image source={require('./path/to/image/loading.gif')} />
    

    <Image source={{uri: 'http://www.urltogif/image.gif'}} />
    

    【讨论】:

    • 如何链接到我的 Images.xcassets 中的 gif?
    • RN 不再使用 Images.xcassets。您需要将它们放在项目的文件夹中,并使用该文件的相对路径。
    • 从 0.13 到 0.14 的主要变化:静态图像。您应该使用 require 和本地图像的相对路径。
    • 据我所知和测试,不适用于 Android。
    【解决方案3】:

    对于 React Native 0.60 及更高版本

    打开您的android/app/build.gradle 文件并将以下行添加到dependencies 部分的第一部分

    implementation 'com.facebook.fresco:fresco:2.0.0'
    implementation 'com.facebook.fresco:animated-gif:2.0.0'
    

    然后

    cd android
    gradlew clean
    react-native run-android
    

    【讨论】:

    • 我刚刚使用了实现 'com.facebook.fresco:animated-gif:2.0.0' 并在 gradlew clean 之后
    • @Bashirpour 我建议您按照 DavideMartina 的建议进行更新
    • 我不得不使用./gradlew clean
    【解决方案4】:

    对于我来说 React native 0.65.1 react-native docs 中的解决方案无效 我必须使用最新版本的 Fresco:

    implementation 'com.facebook.fresco:animated-gif:2.5.0'

    现在我可以在 Android 上运行 GIF。

    您可以通过their website查看最新的 Fresco。

    【讨论】:

    • 很好地抓住了这个版本,android studio 快速操作只更新到 2.0.0 并且仍然没有工作。我不得不手动将其更新到 2.5.0
    • Amazing 差不多要找 2 天了。
    • 你应该得到所有的支持。
    • 谢谢。工作!
    • 这适用于最近的 react-native 版本 0.67.1。谢谢
    【解决方案5】:

    对于 Android,您需要添加 Facebook 的 Fresco 库

    React Native 不提供开箱即用的 Gif 支持,但您可以添加 Facebook 的 Fresco 库来添加此支持。

    您应该能够简单地将以下内容添加到您的 build.gradle 文件中:

    compile 'com.facebook.fresco:animated-gif:0.+'
    

    特定版本兼容性

    如果您遇到问题或想使用静态版本(强烈推荐),您可以直接转到以下 React Native 文档页面,并将 URL 中的 0.46 替换为你正在运行的 React Native 版本:

    https://facebook.github.io/react-native/docs/0.46/image.html#gif-and-webp-support-on-android

    【讨论】:

    • 第二部分(使用静态版本)实际上至关重要,没有使用正确的版本我遇到了崩溃,切换文档中的指定版本修复了它
    • @BlueBot 我只关心静态版本控制。依赖版本中的魔法越少越好。确保最少的意外。
    • 那么,如果 React Native 没有开箱即用的 GIF 支持,为什么它适用于 Apple/IOS?
    • 具有特定版本兼容性的救星
    【解决方案6】:

    如果你想使用 gif 作为背景图片,你可以使用

    <ImageBackground
     source={yourSourceFile}
    > 
     -- your content ---
    </ImageBackground>
    

    【讨论】:

      【解决方案7】:

      要在您的项目中添加 gif 和 WebP,您需要一些可选模块。如果 RN 版本 android/app/build.gradle 文件中添加以下行。

      dependencies {
        // If your app supports Android versions before Ice Cream Sandwich (API level 14)
        compile 'com.facebook.fresco:animated-base-support:1.10.0'
      
        // For animated GIF support
        compile 'com.facebook.fresco:animated-gif:1.10.0'
      
        // For WebP support, including animated WebP
        compile 'com.facebook.fresco:animated-webp:1.10.0'
        compile 'com.facebook.fresco:webpsupport:1.10.0'
      
        // For WebP support, without animations
        compile 'com.facebook.fresco:webpsupport:1.10.0'
      }
      

      如果 RN 版本为 0.60 或更高,则在 android/app/build.gradle 文件中添加以下行

      dependencies {
        // If your app supports Android versions before Ice Cream Sandwich (API level 14)
        implementation 'com.facebook.fresco:animated-base-support:1.10.0'
      
        // For animated GIF support
        implementation 'com.facebook.fresco:animated-gif:1.12.0'
      
        // For WebP support, including animated WebP
        implementation 'com.facebook.fresco:animated-webp:1.10.0'
        implementation 'com.facebook.fresco:webpsupport:1.10.0'
      
        // For WebP support, without animations
        implementation 'com.facebook.fresco:webpsupport:1.10.0'
      }
      

      【讨论】:

      • 太棒了,您在哪里将它添加到您的 Expo 应用程序中?
      • @webmaster 你可以阅读这个forums.expo.io/t/display-animated-gif/4940/6
      • 实际上将 .gif 转换为视频文件 (mp4) 解决了我的问题 ;) 无需导入东西.. :)
      【解决方案8】:

      import React,{useState} from 'react';
      
      **step1 import from react narive  You Can Use (Image) Or (ImageBackground)  **
      
      
      import { StyleSheet, Text, View ,ImageBackground} from 'react-native';
      
      
      
      
      
      function LoadingUsers() {
          return(<View style={styles.LoadingView}>
      
      
      
      **Step 2 require inside source ImageBackground **
      
      
      
      <ImageBackground source={require('../assets/stickman.gif')} style={styles.Gif}><Text>Loading..</Text></ImageBackground>
      </View>)
      }
       
      
      
      **Step 3 Set Width ANd height **
      
      
      const styles = StyleSheet.create({
          LoadingView:{
              flex:1,
          },
          Gif:{
              flex:1,
              width:"100%",
              height:"100%",
              justifyContent:"center",
              alignItems:"center",
              backgroundColor:'#000',
          }
        
          });
          export default LoadingUsers ;

      【讨论】:

        【解决方案9】:

        DylanVann / react-native-fast-image 是一个不错的替代方案,它支持 Android(基于 glide 而不是 fresco)和 iOS(SDWebImage)的 GIF,并具有如下所示的附加功能:

        const YourImage = () => (
            <FastImage
                style={{ width: 200, height: 200 }}
                source={{
                    uri: 'https://unsplash.it/400/400?image=1',
                    headers: { Authorization: 'someAuthToken' },
                    priority: FastImage.priority.normal,
                }}
                resizeMode={FastImage.resizeMode.contain}
            />
        )
        

        【讨论】:

        • aweosme,已经在使用 FastImage,不需要添加 freco
        【解决方案10】:

        对于RN &gt;= 0.66

        编辑您的android/app/build.gradle 文件并添加以下代码:

        implementation 'com.facebook.fresco:fresco:2.0.0'
        implementation 'com.facebook.fresco:animated-gif:2.6.0'
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-02-03
          • 1970-01-01
          • 2019-01-04
          • 2021-06-12
          • 1970-01-01
          • 2016-11-05
          相关资源
          最近更新 更多