我的React Native应用程序在Android版本中的GIF图像无法播放动画

7
我在我的 RN 应用程序的 Android 版本上无法使 GIF 动画正常播放。IOS 版本正在按预期循环播放 GIF,但在 Android 设备上,我只能看到一个卡住的“单帧”图像。
根据调试和 RN 文档,需要在 /android/app/build.gradle 文件的 dependencies 中添加一些 implementation 代码行,但即使在清理 gradle(在/android文件夹中运行 ./gradlew clean)并删除 RN 应用程序的缓存(在项目根目录中运行 react-native start --reset-cache),我在应用程序中没有看到任何区别。
我已经尝试过很多方法,也进行了谷歌搜索。基于我的调试探险,我尝试了并仔细检查了这些建议,这些建议似乎适用于其他人,但对我来说并不起作用...
1. 我尝试了几个版本的 Fresco 库,似乎是必需的,并且还尝试将这些代码行放置在依赖项的底部和顶部。
2. 一些答案还建议在 android/app/proguard-rules.pro 中添加一行或多行代码,但这也没有改变任何东西。
3. 我在应用程序中以不同的方式使用 GIF,但它总是将宽度和高度包含在 的 style 属性中。
4. 我已经尝试过来自不同 CDN 和域的 GIF-uri。
5. 在我的测试设备上重新安装了应用程序。
6. 关闭并重新打开了我的代码编辑器。
我使用以下版本:
Java: 11.0.12 React Native: 0.65 Android SDK: 30.0.2 npm: 6.14.4
这是我的完整 /android/app/build.gradle 文件:
apply plugin: "com.android.application"

import com.android.build.OutputFile


project.ext.react = [
    enableHermes: false,  // clean and rebuild if changing
]

apply from: "../../node_modules/react-native/react.gradle"


def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false
def jscFlavor = 'org.webkit:android-jsc:+'
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://reactnative.dev/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            // Example: versionCode 1 will generate 1001 for armeabi-v7a, 1002 for x86, etc.
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        defaultConfig.versionCode * 1000 + versionCodes.get(abi)
            }

        }
    }
}

dependencies {

    implementation fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }


    implementation project(':react-native-notifications')
    implementation 'com.google.firebase:firebase-core:16.0.0'
    implementation 'com.google.android.gms:play-services-ads:19.8.0'
    implementation "androidx.appcompat:appcompat:1.0.0"

    implementation 'com.facebook.fresco:fresco:2.4.0'
    implementation 'com.facebook.fresco:animated-gif:2.4.0'
    implementation 'com.facebook.fresco:webpsupport:2.4.0'
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply plugin: 'com.google.gms.google-services'

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

如果我漏掉了一些显而易见的东西,请告诉我。我在iOS开发方面的经验更为丰富,所以很可能会错过一些东西:-)


我知道这不是回答问题的方法,但是我使用Lottie动画代替GIF,它们是轻量级动画。请访问https://lottiefiles.com/了解更多信息。 - I.Step
@Atmas,JSX没有包含在内,因为它只是一个基本的Image组件,就像我在问题中描述的那样,我在我的应用程序中使用了50多个这样的组件,在iOS上它们都能正常工作。以下是一个示例: <Image style={{width: '100%', height: '100%'}} source={{uri: 'https://media4.giphy.com/media/8PdWdNWkVOlHJsleC7/giphy.gif?cid=ecf05e4737f206b34f7d9ca68d2d9161b6d1441f435a5a38&rid=giphy.gif&ct=g'}} /> - Simon Degn
你尝试过使用"animated-gif:2.0.0"吗?(具体是版本2.0.0)。较新的Fresco版本似乎在React Native上存在问题 - GitHub问题 - LonelyCpp
@LonelyCpp,版本2.0.0也没有运气。 - Simon Degn
1
刚刚从0.64更新到0.65,同时将animated-gif从2.0.0升级到2.6.0,这解决了gif的问题。 - Hugo Gresse
显示剩余4条评论
6个回答

13

我在使用react-native 0.66.3时遇到了同样的问题,通过将app/build.gradle中的com.facebook.fresco:animated-gif更新为2.6.0解决了我的问题。


1
这个可行,谢谢伙计。 - Premdas v.m
1
谢谢。我花了好几天的时间寻找这个。 - Rahul Padalkar
1
这应该是被接受的答案。救了我! - jakeforaker

8
更简单的解决方法是使用FastImage库,该库内置了GIF支持,并具有缓存等增强功能。

嗨,Muhammed,感谢您的建议。您是否尝试过使用FastImage来解决这个特定问题?为了使用新库,需要花费一些时间来重构我的应用程序,因此我想确保该库不仅仅是在React-Native组件(<Image>)的基础上添加新内容 :-) - Simon Degn
它不是基于ReactNative Image组件构建的。我在我的项目中使用它来显示gif,效果很好。 - Muhammed Yasir MT
嗨,Muhammed,我又来了。我尝试了FastImage库,它完美地解决了我的问题!它还解决了我的另一个问题,即在iOS和Android上重新渲染屏幕时<Image />会快速闪白的问题。谢谢 :-) - Simon Degn

2

Android支持GIF和WebP格式

在构建自己的本地代码时,Android默认不支持GIF和WebP格式。

根据您的应用程序需求,您需要在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.3.0'

  // For animated GIF support
  implementation 'com.facebook.fresco:animated-gif:2.5.0'

  // For WebP support, including animated WebP
  implementation 'com.facebook.fresco:animated-webp:2.5.0'
  implementation 'com.facebook.fresco:webpsupport:2.5.0'

  // For WebP support, without animations
  implementation 'com.facebook.fresco:webpsupport:2.5.0'
}

您可以查看官方文档 链接 了解关于 Android 平台上 GIF 和 WebP 格式的支持。

1
我自己在安卓系统上遇到了GIF的问题,最终转而使用动态WebP格式,谷歌似乎很支持这种格式,并提供了自己的转换工具。多年来,他们一直支持安卓系统使用该格式,并且似乎更加稳定。将扩展名改为WebP而不是PNG可能会起作用。

https://frescolib.org/docs/webp-support.html


嗨,感谢您的建议。我的应用程序中的内容是由用户生成的,因此我不能强制用户使用特定的文件类型,因为存在内容遗留问题 :-) - Simon Degn

0

1
嗨iyoda,欢迎来到Stackoverflow :-) 如我的问题(和代码)所述,我已经在我的build.gradle文件中添加了fresco:animated-gif。我还尝试过版本1.0和1.3,但都没有成功。 - Simon Degn
对不起,我没有仔细阅读它。- @SimonDegn - iyoda

0

你只需要使用react-native-fast-image库..

<FastImage source={{ uri: img, // headers: { Authorization: 'someAuthToken' }, priority: FastImage.priority.normal, }} resizeMode={FastImage.resizeMode.cover} onError={() => console.log( 'image doest not exist or look like http:', ) } style={{ width: '100%', height: '100%', borderRadius: 10, }} />


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接