[英]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
将管理不断变化的图像。
This component needs to take a few props: 该组件需要采取一些道具:
firstImage
this is the uri
to the first image that it will show. firstImage
这是它将显示的第一个图像的uri
。 nextImage
this is the uri
to the next image that it will show. nextImage
这是它将显示的下一个图像的uri
。 order
this is a number representing when the image should change. order
这是一个数字,表示何时应更改图像。 eg 6
means it would change after 30 seconds 6
表示30秒后它将改变 totalImages
this is a number representing the number of images you are using, eg 10
totalImages
这是一个数字,代表您正在使用的图像数,例如10
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
,否则可能会导致内存泄漏。
In this component you will want to construct three arrays. 在此组件中,您将要构造三个数组。
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.