目前RN在Android平台上不支持gif格式的图片,而在iOS平台是支持的,期待以后的版本中系统也是可以默认支持android的。首先说下在ios平台怎么加载gif呢?http://blog.csdn.net/xiangzhihong8/article/details/55803237?1487761206687
<Image source= {require('./img/loading.gif')} style = {styles.loading}/> 1 1完整实例:
xport default class Loading extends React.Component { render(){ if (!this.props.isShow) { return <View /> } return ( <View style = {styles.container}> <Image source= {require('./img/loading.gif')} style = {styles.loading}/> </View> ) }; } const styles = StyleSheet.create({ container:{ backgroundColor: 'transparent', position: 'absolute', top: 0, left: 0, height: Util.screenSizeUtil.height, width: Util.screenSizeUtil.width, alignItems: 'center', justifyContent: 'center', }, loading:{ height:30, width:30, }, }) 1234567891011121314151617181920212223242526272829303132 1234567891011121314151617181920212223242526272829303132要解决上面的问题,方法还是很多的,最简单的莫过于使用facebook的jar支持库,在android/app/build.gradle文件中新增
compile 'com.facebook.fresco:animated-gif:0.13.0' 12 12Fresco是一个强大的图片加载组件,Android 本身的图片库不支持此格式,但是Fresco支持。使用时,和往常一样,仅仅需要提供一个图片的URI即可,剩下的事情,Fresco会处理。 如我们运行一个名为loading.gif的图片:
<Image source={{uri:loading}} style={{width:20,height:20}}/> 1 1当然网上还有另外的方法,就是自己去实现读取gif图片,对图片资源做拆解,这有点类似于,在很久以前,Android平台也是不支持gif的,出现了自定义view对gif图片进行拆解,然后运行image的方案。有点类似于Android的帧动画,在xml定义图片数组,然后使用Animator来加载。不过这种方法性能差。
将gif切成15张png的图片,暂且命名为loading1、loading2…. loading15。
每隔100毫秒切换一张图片,当数据加载完毕,清楚定时任务,并且将图片置为第一张。
图片轮播函数 _loop() { this.loopCount++; if (this.loopCount < loading_imgs.length) { this.setState({ imageIndex: this.loopCount, }); }else { this.loopCount = -1; } } //轮播图片 this.timerPic = setInterval(this._loop.bind(this), 100); //清除图片轮播效果 this.timer1 && clearInterval(this.timer1); this.loopCount = -1; 123456789101112131415161718 123456789101112131415161718这样就实现了自己实现对gif运行的实现,不过其性能确实太差,建议使用第一种。
附:RN知识库
