react-native - 如何在本机反应中使用 uri 播放 Lottie 动画?
问题描述
我如何从服务器(uri)播放 Lottie 动画?我可以从我的项目资产中播放动画,但是当我使用 source={{uri:"https://...data here..."}} 时,我遇到了一个错误...即使我将 json 文件下载到手机并尝试然后从手机存储中播放,但也遇到了错误!这里的代码:
<LottieView style={{ width: "28%", marginTop: "5%", alignContent: "center", alignItems: "center",
alignSelf: "center" }}
source={{uri:"file://///data/user/0/com.duststudio/files/vv/test2.json"}}
autoPlay
loop />
我真的需要它...如果有人知道该怎么做,我会非常高兴和感激:)
解决方案
我找到了...我刚刚找到了方法 :') 你只需要获取 Lottie Animation 的 JSON 然后为它设置一个状态,看看这个:
import React, { useState, useEffect } from 'react';
import { Text, View, TouchableOpacity } from 'react-native';
import LottieView from 'lottie-react-native';
export default function App(){
const [LottieAnim, setLottieAnim] = useState()
useEffect(() => {
fetch('https://test.ir/test.json', {
method: "GET",
})
.then((response) => response.json())
.then((responseData) => {
console.log(responseData);
setLottieAnim(responseData)
})
.catch((error) => {
console.log(error);
})
}, [])
return(
<View style={{ flex: 1 }}>
{LottieAnim ? <LottieView source={LottieAnim} style={{ width: "50%" }} autoPlay loop /> : (null)}
</View>
)}
推荐阅读
- r - R在删除除一列之外的所有列后保留列名
- php - 带有 Curl Oauth1 的 Cardinity API
- android - isWallsupportSupported() 返回 false
- python-3.x - 如何从 pytest 引用我的 Azure 函数?
- bash - 对列中的字符进行计数和索引
- android - Gradle 找不到 Android Compose 编译器
- php - 如何解决此错误消息“找不到合适的服务器(`serverSelectionTryOnce` 集):[无法解析 'db'] 在 Lumen 上?
- performance - Docker 中跨微服务的性能测试
- telerik - 为什么分页在此 RadGrid 上不起作用
- gitlab - 如何将 NPM 包发布到 Gitlab 子组内的项目中