从图像池中选择背景图像

问题描述 投票:2回答:1

我是新来的本地反应,我想知道如何编写/构建应用程序的起始页面,每次启动应用程序时都会显示不同的图像(对于同一个用户)?

我有一组可用的图像,我希望每次用户启动应用程序时,起始页面都会显示不同的随机背景图像。

reactjs react-native
1个回答
3
投票

您可以在数组中设置BackgroundImages uri,并在0 to array length之间使用随机生成器并渲染它。

const Images = ['uri 1', 'uri 2', 'uri 3', 'uri 4']

componentDidMount() {
  const randomNumber = Math.floor(Math.random() * Images.length);
  this.setState({currentImageIndex: randomNumber})
}

render() {


    return (
        <ImageBackground source={{uri: Images[this.state.currentImageIndex]}}>
            //... Other stuff
        </ImageBackground>
      )
    }
© www.soinside.com 2019 - 2024. All rights reserved.