React Native SVG Animated 绘制动画

14 篇文章 0 订阅
 
 

转自:React Native SVG描边动画

stroke属性

stroke 定义描边颜色

stroke="#0078ff"

strokeWidth 定义描边的宽度

strokeWidth="3"

创建虚线时候用到的属性:

strokeDasharray 创建虚线

// 假设一个‘空格’5像素,一个‘_’5像素

strokeDasharray="5"  
// 看到的是 _ _ _ _ 即5像素实线,5像素空白,5像素实线,.... 循环

strokeDasharray="5,10"  
// 看到的是 _  _  _  _  5像素实线,10像素空白...循环

strokeDashoffset 虚线的位移

strokeDasharray="5"
strokeDashoffset="1"
// 看到的第一个_会往左边挪动1像素

描边动画

有足够长的空白,控制位移,让空白的位置移动,实线的位置就会慢慢显示出来。
假设灰色区域宽度300:

Untitled 3.png

如果strokeDashoffset从300到0连续变化,就是描边了

有一小段实线在奔跑

有足够长的空白,有一小段实线,改变strokeDashoffset控制空白的移动

Untitled 2.png

如果strokeDashoffset从20到320连续变化,会看到小段实线从右向左滑动过来。画曲线就可以跑曲线了。

跑个栗子

在react-native里,可以用animated改变strokeDashoffset的值来做动画。
另:类似sketch等软件可以绘矢量图并存成svg格式,基本上改改就能用了比较方便。

rn

npm install --save react-native-svg

使用react-native里的ART的话,strokeDasharray和strokeDashoffset写在strokeDash属性里。
一个不是例子的栗子:

import {
    Animated 
    ...
} from 'react-native';

import Svg, {
    G,
    Path
} from 'react-native-svg';

// 注意要用 Animated.createAnimatedComponent 让组件可动画化
let AnimatePath = Animated.createAnimatedComponent(Path);

...
    constructor (props) {
        super(props);

        this.state = {
            strokeDashOffset: new Animated.Value(38)
        }
    }

    changeStroke = () => {
        // 使用炫酷的animated让小线段愉悦的奔跑��
        Animated.spring(
            this.state.strokeDashOffset,
            {
                toValue: 243
            }
        ).start();
    };

    render () {

        return (
            <View>
                ...
                <TouchableWithoutFeedback
                    onPress={this.changeStroke}
                >
                    <Svg
                        height="100"
                         width="100"
                    >
                        <G fill="none">
                            <AnimatePath
                                d={一坨路径}
                                stroke="#0078FF"
                                strokeWidth="3"
                                strokeDasharray="28,215"
                                strokeDashoffset={this.state.strokeDashOffset}
                               />
                           </G>
                      </Svg>
                </TouchableWithoutFeedback>
            </View>
        )
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值