【问题标题】:React-native image with animated background as loader具有动画背景的 React-native 图像作为加载器
【发布时间】:2018-05-26 11:11:00
【问题描述】:

我有一个平面列表,除其他外,它呈现带有叠加层的图像。

我创建了一个动画背景,当图像加载时会改变颜色(工作正常)。但是一旦加载图像,我正在努力使背景消失。我尝试使用 onLoad 但设置 this.state 适用于列表中的所有图像。如果图像在加载之前隐藏起来会更好。我更喜欢实时延迟加载,而不是在 ComponentWillMount 中预取它们。

有什么想法吗?

<Image
    source={{uri: image}}
    style={styles.cardImg}
    onLoad={() => this.state.imageLoaded = true}
/>
{!this.state.imageLoaded && (
    <Animated.View
        style={[styles.cardBg, {
            backgroundColor: this._animatedValue.interpolate({
                inputRange: [0, 100],
                outputRange: ['rgba(100,100,100, 0.3)', 'rgba(100,100,100,0.5)']
            })
        }]}
    />
)}

更新

<FlatList
          ref={(ref) => { this.FlatList = ref; }}
          data={this.props.data}
          keyExtractor={(item) => item._id}
          renderItem={this._renderCard.bind(this)}
          onEndThreshold={50}
          onEndReached={this.props.loadMore}
          ListFooterComponent={this._renderFooter}
          showsVerticalScrollIndicator={false}
          style={styles.list}
          extraData={this.state}
          refreshControl={<RefreshControl
                    refreshing={this.props.loading}
                    onRefresh={this.props.onRefresh}
                />}
        />

更新 2

理想情况下,我正在寻找类似的东西(代码不起作用):

<Image source={{uri: image}} style={styles.cardImg} onError={(e) => {card.item.media.error = true}}/> 

【问题讨论】:

  • this.state.imageLoaded = true 为此使用 setState。请提供您正在使用的列表的代码。
  • 要回答你的问题,我需要了解 _renderCard 函数在做什么。
  • 其中的代码包括:
  • 那么你可能需要修复那些其他的东西才能让它工作。

标签: reactjs react-native react-native-android react-native-ios


【解决方案1】:

这就是Component闪耀的地方:

class LoadingImage extends Component {
  constructor(props) {
    super(props);

    this.state = {
      imageLoaded: false
    }
  }

  render() {
    /// delegate every props as Image's props
    return (
      <Image
        {...this.props}
        onLoad={() => this.setState({imageLoaded: true})}
      />
      {!this.state.imageLoaded && (
        <Animated.View
          style={[styles.cardBg, {
            backgroundColor: this._animatedValue.interpolate({
              inputRange: [0, 100],
              outputRange: ['rgba(100,100,100, 0.3)', 
                            'rgba(100,100,100,0.5)']
            })
          }]}
        />
      )}
    );
  }
}

所以你可以在你的 App 中使用这个 LoadingImage。

<LoadingImage
    source={{uri: image}}
    style={styles.cardImg}
/>

【讨论】:

  • 谢谢!这是向前迈出的一大步,但问题仍然存在,如何在我知道是否有错误之前禁用渲染图像?现在,由于我在样式中的图像大小,它会短暂显示两者
  • 我在componentWillMount中实现Image.prefetch!再次感谢您!
猜你喜欢
  • 2016-03-31
  • 2019-12-16
  • 1970-01-01
  • 1970-01-01
  • 2021-04-10
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多