简体   繁体   English

使用 react-native-svg 动画静态 SVG 文件

[英]Animate static SVG file with react-native-svg

I'm struggling to get react-native-svg animate in React Native.我正在努力在 React Native 中获得react-native-svg动画。 I also tried the method they suggest with react-native-svg-transformation but it didn't suit my case since I'm working with many files and render them dynamically.我还尝试了他们建议的react-native-svg-transformation但它不适合我的情况,因为我正在处理许多文件并动态呈现它们。 Here's my render file:这是我的渲染文件:

import React from "react";
import { View, Text } from "react-native";
import { SvgXml } from "react-native-svg";
import SvgAssets from "../resources/SvgAssets";
import AssetsHelper from "../common/AssetsHelper";

class ChineseCharacter extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      xmlData: "",
    };
  }

  render() {
    const { xmlData, file } = this.state;
    if (xmlData.length === 0) {
      return <View />;
    }

    return (
      <View style={{ flex: 1 }}>
        <SvgXml xml={xmlData} width="200" height="200" />
      </View>
    );
  }

  componentDidMount(): void {
    const { character } = this.props;
    const characterUnicode = character.charCodeAt(0);
    const file = SvgAssets[characterUnicode.toString()];
    AssetsHelper(file)
      .then(result => {
        this.setState({
          xmlData: result,
        });
      })
      .catch(err => console.log(err));
  }
}

export default ChineseCharacter;

AssetsHelper is basically reading the svg file and convert them to string in order to pass to SvgXml . AssetsHelper基本上是读取 svg 文件并将它们转换为字符串以传递给SvgXml SvgAssets is an object with key as the charCode and value is the file, something like this: SvgAssets是一个对象,键是 charCode,值是文件,如下所示:

const assets = {
  "11904": require("./svgs/11904.svg"),
  ...
}

Thank in advance.预先感谢。

After few struggling hours, I have found a work around for this problem.经过几个小时的努力,我找到了解决这个问题的方法。 I don't use react-native-svg anymore, instead I parse the .svg file to string and put it in react-native-webview .我不再使用react-native-svg ,而是将 .svg 文件解析为字符串并将其放入react-native-webview Work like a charm!像魅力一样工作!

render() {
    // @ts-ignore
    const { xmlData, file } = this.state;
    if (xmlData.length === 0) {
      return <View />;
    }
    return (
      <View style={{ width: 300, height: 300 }}>
        <WebView
          source={{ html: xmlData }}
          style={{ backgroundColor: "transparent", width: 300, height: 300 }}
        />
      </View>
    );
  }

Try to import the svg files inside ChineseCharacter class.尝试在ChineseCharacter类中导入svg文件。

import svgXml11904 from './svgs/11904.svg'

const assets = {
  "11904": svgXml11904,
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM