React-Native ListView加载图片淡入淡出效果的组件

今天练习项目中需要给listview在加载图片时增加一个淡入淡出的效果,因此干脆就自己封装了一个组件:

 1 'use strict'
 2 
 3 import React from 'react-native'
 4 
 5 var {
 6   Animated,
 7   PropTypes
 8 } = React
 9 
10 class AniImage extends React.Component {
11   static propTypes = {
12     url: PropTypes.string,
13     inputRange: PropTypes.array,
14     outputRange: PropTypes.array
15   };
16   render () {
17     var { style, url, inputRange, outputRange } = this.props
18     this._animatedValue = new Animated.Value(0)
19     let interpolatedColorAnimation = this._animatedValue.interpolate({
20       inputRange: inputRange,
21       outputRange: outputRange
22     })
23     return (
24       <Animated.Image
25         onLoadEnd={() => {
26           Animated.timing(this._animatedValue, {
27             toValue: 100,
28             duration: 500
29           }).start()
30         }}
31         source={{uri: url}}
32         style={[style, {opacity: interpolatedColorAnimation}]} />
33     )
34   }
35 }
36 
37 module.exports = AniImage

那么如何调用呢?

一、导入 AniImage

二、调用

1 <AniImage
2  inputRange={[0, 100]}
3  outputRange={[0, 1]}
4  style={styles.aniImage}
5  url={'http://192.168.6.5:8888/getImage?imgName=' + commidities.imgPath1} />

这样就看到想要的效果啦。

https://github.com/weifengzz/GuoKu

在github上可以看到我的完整项目哦。 

原文地址:https://www.cnblogs.com/weifengzz/p/5165862.html