Skip to content
This repository has been archived by the owner on Apr 26, 2022. It is now read-only.

πŸ’ƒπŸ» Display Animated WebP images in React Native the hacky way, support for Android & iOS both

License

Notifications You must be signed in to change notification settings

junhoyeo/react-native-animated-webp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ’ƒπŸ» react-native-animated-webp

example animated player
npm version weekly downloads npm bundle size
NPM

Display and control Animated WebP images in React Native the hacky way

πŸ“¦ Installation

πŸ‘‹ PeerDependencies first

This module uses react-native-fast-image for rendering images, so you should install it first besides with react and react-native.

npm install react-native-fast-image
# Or using yarn
yarn add react-native-fast-image

πŸ™Œ The package

npm install react-native-animated-webp
# Or using yarn
yarn add react-native-animated-webp

🍭 For Android

Add dependencies for fresco, which adds native support for Animated WebP at android/app/build.gradle.

Since FastImage is not working well for Animated WebP(maybe issue of Glide?), we internally use the native Image component for android platforms.

dependencies {
+  implementation 'com.facebook.fresco:fresco:2.0.0'
+  implementation 'com.facebook.fresco:animated-webp:+'
+  implementation 'com.facebook.fresco:webpsupport:+'

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

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

ο£Ώ For iOS

Add the following three lines inside your project's ios/{YourAppName}/AppDelegate.m file for Animated WebP support.

+ #import "SDImageCodersManager.h"
+ #import <SDWebImageWebPCoder/SDImageWebPCoder.h>

οΉ£ (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // . . .

+    [SDImageCodersManager.sharedManager addCoder:SDImageWebPCoder.sharedCoder];

    // . . .
}

πŸ₯ Usage

import React, { useRef } from 'react';
import { Button, View } from 'react-native';
import AnimatedPlayer, { IAnimatedPlayerReference } from 'react-native-animated-webp';

import thumbnailImage from '../assets/thumbnail.png';
import animatedWebPImage from '../assets/animated.webp';

const App: React.FC = () => {
  const playerRef = useRef<IAnimatedPlayerReference>();

  const onPressButton = () => {
    if(!playerRef.current?.isPlaying) {
      playerRef.current?.play(() => console.log('callback after play ended'));
    }
  };

  return (
    <View style={{ flex: 1 }}>
      <AnimatedPlayer
        ref={playerRef}
        thumbnailSource={thumbnailImage}
        animatedSource={animatedWebPImage}
        duration={500}
        autoplay={false}
        loop={false}
      />
      <Button
        title="Click Me"
        onPress={onPressButton}
      />
    </View>
  );
};