简体   繁体   English

反应本机更改图像源

[英]React Native change source of image

I have 10 Image component in a view and I need to change the source of image every 5000 ms. 我在视图中有10个Image组件,我需要每5000毫秒更改一次图像源。 One problem is I have to change them one at a time in random. 一个问题是我必须一次随机更改一个。 For example Image 1 will change, after 5000ms Image 6 will change, after 5000ms Image 3 will change and so on. 例如,图像1将更改,在5000毫秒后图像6将更改,在5000毫秒后将更改图像3,依此类推。 Is there a way to do this? 有没有办法做到这一点?

render() {

let image_containers = []

for(let i = 0; i < 30; i++){

    let rand = Math.floor(Math.random() * 29 + 0)

    image_containers.push(<Animated.Image

                      key={i}
                      style={{
                                  opacity:1,
                                  margin:'.11%',
                              width: '33%',
                              height: 90}}
                          source={{uri: this.state.images_random[i]}}

                  />);

}

return (

    <View style={styles.image_container}>

        {image_containers}

    <View style={styles.overlay}></View>

    </View>

)

} }

It's an intriguing problem to solve. 这是一个有趣的问题。 If you have all the images already then it makes it much easier. 如果您已经拥有所有图像,那么它将变得更加容易。 Here is a possible solution. 这是一个可能的解决方案。

I would start by having a parent component that will do most of the heavy work in constructing the arrays needed. 我将从拥有一个父组件开始,该父组件将在构造所需的数组方面进行大部分繁重的工作。 Let's call it GridImageView and then a child component called ChangingImage that will manage the changing images. 让我们把它GridImageView然后一个子组件叫ChangingImage将管理不断变化的图像。

ChangingImage 更改图片

This component needs to take a few props: 该组件需要采取一些道具:

  1. firstImage this is the uri to the first image that it will show. firstImage这是它将显示的第一个图像的uri
  2. nextImage this is the uri to the next image that it will show. nextImage这是它将显示的下一个图像的uri
  3. order this is a number representing when the image should change. order这是一个数字,表示何时应更改图像。 eg 6 means it would change after 30 seconds 例如6表示30秒后它将改变
  4. totalImages this is a number representing the number of images you are using, eg 10 totalImages这是一个数字,代表您正在使用的图像数,例如10
  5. lastImageShown this is function that will call back to the parent component so that it knows all the images have been shown (that is why we need the previous prop) lastImageShown这是一个函数,它将调用父组件,以便知道所有图像都已显示(这就是我们需要上一个道具的原因)

This component is basically a wrapper around Image you can do a lot of things. 这个组件基本上是Image的包装,您可以做很多事情。 In here the basic idea is that each ChangingImage will have perhaps a setTimeout that will fire after a specific amount of time caused by the order prop, ie order * 5000 . 在这里的基本思想是每个ChangingImage将有也许 setTimeout引起特定的时间量之后,将触发order支柱,即order * 5000 If the order greater than the totalImages then you will want to tell the GridImageView that all images have now changed and you should perform a re-render to start the process again. 如果order大于totalImages那么您将要告诉GridImageView所有图像现在都已更改,应该执行重新渲染以再次开始该过程。 Remember to clearTimeout if your component unmounts otherwise you could get memory leaks. 如果卸载组件,请记住clearTimeout ,否则可能会导致内存泄漏。

GridImageView GridImageView

In this component you will want to construct three arrays. 在此组件中,您将要构造三个数组。

  1. The original images to show, and array of uris. 要显示的原始图像和uris数组。
  2. The randomized images to show, a randomized array of uris. 要显示的随机图像,尿素的随机数组。
  3. The order the images should be shown, order should be >= 1, this should be randomized too. 图像显示的顺序,顺序应> = 1,也应将其随机化。

Using a similar approach to what you were doing above you could construct an array of ChangingImages passing in the required props. 使用类似的方法给你上面,你在做什么,可以构建阵列ChangingImages传递所需要的道具。

You will probably want to limit the re-rendering of this component as a full re-render could cause all its children to re-render losing what you accomplished. 您可能希望限制此组件的重新渲染,因为完全重新渲染可能会导致其所有子代重新渲染而失去您完成的工作。

I hope that this gives you some ideas on how you could proceed, or even perhaps a way not to do it. 我希望这会给您一些有关如何进行的想法,甚至可能是不这样做的一种方法。 It is definitely one solution and it can really be expanded upon as there are lots of things you could do with this. 这绝对是一种解决方案,并且可以扩展,因为您可以用它做很多事情。

It's simple. 这很简单。

Your code of image component should be like this: 您的图片组件代码应如下所示:

<Image source={require('./my-icon.png')} />

but also you should have an array of images paths. 而且您还应该具有图像路径数组。 For example: 例如:

let images = ['./my-icon-1.png', './my-icon-2.png', './my-icon-3.png']

And all your need is change the image path inside require statement: 而您所需要做的就是更改require语句中的图像路径:

<Image source={require(images[0])} />

or 要么

<Image source={require(images[2])} />

Just paste any image index instead of 0 or 2 every 5000 ms. 只需每5000 ms粘贴任何图像索引而不是0或2。

That's all! 就这样! :) :)

Also, please read the RN guide: https://facebook.github.io/react-native/docs/images 另外,请阅读RN指南: https : //facebook.github.io/react-native/docs/images

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM