【发布时间】:2019-12-17 11:33:34
【问题描述】:
我正在尝试使用 SVG 图像在 ReactNative expo 中制作一个脉动的 svg 心脏。
我设法使心脏用动画值调整大小的唯一方法是将其绑定到样式:fontSize。
这似乎正确地改变了大小,但动画真的很不稳定。
代码如下:
import React, { Component } from 'react';
import { Animated } from 'react-native';
import { SimpleLineIcons } from '@expo/vector-icons';
const AnimatedIcon = Animated.createAnimatedComponent(SimpleLineIcons);
const TARGET_FONT_SIZE = 16;
const GROWN_FONT_SIZE = 24;
class GrowingHeart extends Component<any, any> {
size = new Animated.Value(TARGET_FONT_SIZE);
constructor(props) {
super(props);
Animated.sequence([
Animated.timing(this.size, {
duration: 1000,
toValue: GROWN_FONT_SIZE
}),
Animated.timing(this.size, {
duration: 1000,
toValue: GROWN_FONT_SIZE
})
]).start();
}
render() {
return (
<AnimatedIcon
style={{ fontSize: this.size }}
size={20}
name="heart"
color="red"
/>
);
}
}
我也尝试绑定宽度和高度,但它们也不稳定 + 它们会更改容器大小,而不是图标。
有没有更好的方法来做到这一点?谢谢
【问题讨论】:
-
你试过用lottie代替svg吗?因为 rn 没有为 svg 提供很多选项
-
我还没有。我正在尝试尽可能使用原始解决方案
标签: react-native animation svg expo react-animated