我是ReactNative的新手,我有以下组件:
<View style={{flex: 1}}>
<View style={styles.container}>
<Animated.Image style= {styles.photo}
source={{uri: this.props.user.picture.large}}/>
</View>
<View style={{backgroundColor:'whitesmoke', flex: 1}}></View>
</View>
在样式中,我有以下内容:
const styles = StyleSheet.create({
container: {
flex: 0.5,
backgroundColor: '#4dbce9',
alignItems: 'center',
},
photo: {
height: 150,
width: 150,
borderRadius: 75,
borderWidth: 3,
borderColor: 'white',
transform: [ // `transform` is an ordered array
{scale: this.state.profileImgBounceVal},
]
}
});
当我访问this.state.profileImgBounceVal
因为它在我知道的组件之外时,我收到了一个错误。除了包括Animated.Image
标记内的样式吗?
答案 0 :(得分:6)
您可以使用Stylesheet.flatten()
在组件中创建可重复使用的样式对象:
var animatedImageStyle = StyleSheet.flatten([
styles.photo,
{
transform: [{scale:this.state.profileImgBounceVal}]
}
])
<View style={{flex: 1}}>
<View style={styles.container}>
<Animated.Image style={animatedImageStyle}
source={{uri: this.props.user.picture.large}}/>
</View>
<View style={{backgroundColor:'whitesmoke', flex: 1}}></View>
</View>