ReactNative系列之四十实现帧动画

本文介绍了一种解决React Native中帧动画首次播放出现白屏现象的方法。通过预加载图片,避免了state快速切换导致的显示延迟,确保动画流畅播放。文章详细解释了使用Animated和Image组件实现这一解决方案的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

不知道有没有更好的方式,请大家回评:

// 第一次播放时,会有白屏现象,应该是由于state过快切换导致第一次require图片时不及时导致。所以可以先用一个宽高都为0的Image先预加载一下需要显示的帧动画~~~即可解决这个问题

import React, {Component} from 'react';

import {
    Animated,
    Easing,
    Image,
} from 'react-native';

import * as TabIcon from "../constants/TabIcon";

export default class FrameAnimation extends Component {

    constructor(props) {
        super(props);
        this.state = {
            frameValue: new Animated.Value(1),
            imageSource: 1,
        }
    }

    _startAnimation(toValue, duration) {
        // this.state.frameValue = new Animated.View(0);
        this.state.frameValue.setValue(0);
        this._animated = Animated.timing(
            this.state.frameValue,
            {
                toValue: toValue,
                duration: duration,
                easing: Easing.linear,
            }
        );
        this._animated.start(() => {
        });
        this.state.frameValue.addListener((event) => {
            let nextImage = parseInt(event.value);
            if (nextImage !== 0 && nextImage !== this.state.imageSource) {
                this.setState({
                    imageSource: parseInt(event.value),
                });
            }
        });
    }

    render() {
        let source = TabIcon.MESSAGE_NORMAL[this.state.imageSource];
        return(
            <Image style={{height: 31, width: 26}} source={source}/>
        );
    }

    componentDidMount() {
        this._startAnimation(8, 7*33);
    }

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值