英文:
Loop through multiple images to create an animation in react native
问题
I have tried looking into multiple tutorials for the same but I could not find the same.
我已尝试查看多个相似的教程,但未能找到相同的方法。
I have multiple images in assets like :
我在资源中有多张图片,如下:
const animImages = [
require('../../img/ic_1.png'),
require('../../img/ic_2.png'),
require('../../img/ic_3.png'),
require('../../img/ic_4.png'),
require('../../img/ic_5.png'),
require('../../img/ic_6.png'),
];
export default animImages;
What I want to do is based on a button press, I wanted to loop these images one by one and show it inside a view. I created a variable and tried to conditionally render the component based on the same. Here is the code snippet.
我想根据按钮的按压,逐个循环这些图片并在视图中显示它们。我创建了一个变量,并尝试根据它来有条件地渲染组件。以下是代码片段。
<>
{animImages.map(img => <Image source={img} />)}
</>
This lets me render all the images, however, I want to loop over the images and render them 1 by 1 so that it looks like an animation. Can anyone help with the same.
这段代码允许我渲染所有图片,但我想循环这些图片并逐个渲染它们,以便看起来像动画。有人可以帮助吗?
Thanks
谢谢
EDIT : I want to loop the images at the same place where the button was, I can take care of the same using the conditional rendering. But I want it to loop for certain seconds and then redisplay the button.
编辑:我想在与按钮相同的位置循环显示这些图片,我可以使用条件渲染来处理它。但我希望它循环一定的时间,然后重新显示按钮。
Here is what I have :
以下是我的代码:
{componentProps.showAnimation ?
<View style={styles.elevatedView}>
<TouchableOpacity
onPress={() => {
console.log("button pressed");
// stop the image loop
}}
>
{The loop for images comes here }
</TouchableOpacity>
</View>
:
<View style={styles.elevatedView}>
<IconsRender
icon={acomponentProps.icons.icon}
/>
</View>
}
【请注意,代码部分保留原文,没有翻译。】
英文:
I have tried looking into multiple tutorials for the same but I could not find the same.
I have multiple images in assets like :
const animImages = [
require('..//../img/ic_1.png'),
require('..//../img/ic_2.png'),
require('..//../img/ic_3.png'),
require('..//../img/ic_4.png'),
require('..//../img/ic_5.png'),
require('..//../img/ic_6.png'),
];
export default animImages;
What I want to do is based on a button press, I wanted to loop these images one by one and show it inside a view. I created a variable and tried to conditionally render the component based on the same. Here is the code snippet.
<>
{animImages.map(img => <Image source={img} />)}
</>
This lets me render all the images, however, I want to loop over the images and render them 1 by 1 so that it looks like an animation. Can anyone help with the same.
Thanks
EDIT : I want to loop the images at the same place where the button was, I can take care of the same using the conditional rendering. But I want it to loop for certain seconds and then redisplay the button.
Here is what I have :
{componentProps.showAnimation ?
<View style={styles.elevatedView}>
<TouchableOpacity
onPress={() => {
console.log("button pressed");
// stop the image loop
}}
>
{The loop for images comes here }
</TouchableOpacity>
</View>
:
<View style={styles.elevatedView}>
<IconsRender
icon={acomponentProps.icons.icon}
/>
</View>
}
答案1
得分: 1
以下是代码的翻译部分:
import React from 'react';
import { StyleSheet, View, Dimensions, Button } from 'react-native';
import FadingSlides from './FadeImage/fadeimage';
const { width, height } = Dimensions.get('window');
const slides = [
{
image: require('./assets/snack-icon.png'),
imageWidth: width - width * 0.3,
imageHeight: width - width * 0.3,
title: 'JavaScript',
subtitle: 'The definitive language',
titleColor: '#fff',
subtitleColor: 'yellow',
},
{
image: require('./assets/snack-icon.png'),
imageWidth: width - width * 0.3,
imageHeight: width - width * 0.3,
title: 'ReactJS',
subtitle: 'JavaScript coolest library',
titleColor: '#fff',
subtitleColor: 'cyan',
},
];
export default App = () => {
const [startAnimation, setStartAnimation] = React.useState(true);
return (
<View style={styles.container}>
<FadingSlides
slides={slides}
fadeDuration={1200}
stillDuration={2000}
startAnimation={startAnimation}
height={height}
/>
<Button
title="Toggle animation"
onPress={() => {
setStartAnimation(() => {
return !startAnimation;
});
}}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ff0000',
},
});
希望这对你有所帮助。
英文:
That is straightforward you should be able to implement that on your own.
Nevertheless I am gonna provide an example based on (https://github.com/chagasaway/react-native-fading-slides)
import React from 'react';
import { StyleSheet, View, Dimensions, Button } from 'react-native';
import FadingSlides from './FadeImage/fadeimage';
const { width, height } = Dimensions.get('window');
const slides = [
{
image: require('./assets/snack-icon.png'),
imageWidth: width - width * 0.3,
imageHeight: width - width * 0.3,
title: 'JavaScript',
subtitle: 'The definitive language',
titleColor: '#fff',
subtitleColor: 'yellow',
},
{
image: require('./assets/snack-icon.png'),
imageWidth: width - width * 0.3,
imageHeight: width - width * 0.3,
title: 'ReactJS',
subtitle: 'JavaScript coolest library',
titleColor: '#fff',
subtitleColor: 'cyan',
},
];
export default App = () => {
const [startAnimation, setStartAnimation] = React.useState(true);
return (
<View style={styles.container}>
<FadingSlides
slides={slides}
fadeDuration={1200}
stillDuration={2000}
startAnimation={startAnimation}
height={height}
/>
<Button
title="Toggle animation"
onPress={() => {
setStartAnimation(() => {
return !startAnimation;
});
}}
/>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#ff0000',
},
});
答案2
得分: 1
你可以真的使用简单的React Native动画来实现这个。一个示例可能是:
import React, { useState, useEffect, useRef } from 'react';
import { Animated, Image, StyleSheet, View, Text } from 'react-native';
const ANIMATION_DURATION = 500; // 你可以根据需要设置动画时长,单位为毫秒
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
image: {
width: 50,
height: 20,
},
});
const IMAGES = [
// 在这里添加你的图片
];
const micAnimation = () => {
const animation = useRef(new Animated.Value(0)).current;
const [imageIndex, setImageIndex] = useState(0);
useEffect(() => {
Animated.loop(
Animated.timing(animation, {
toValue: 1,
duration: ANIMATION_DURATION,
useNativeDriver: true,
}),
).start();
}, []);
animation.addListener(({ value }) => {
const newIndex = Math.floor(value * IMAGES.length);
setImageIndex(newIndex);
});
const imageSource = IMAGES[imageIndex];
return (
<View style={styles.container}>
<Animated.Image
style={[styles.image, { opacity: animation }]}
source={imageSource}
/>
</View>
);
};
export default micAnimation;
英文:
You could really use the simple react native animation to achieve that. An example would be :
import React, { useState, useEffect, useRef } from 'react';
import { Animated, Image, StyleSheet, View , Text} from 'react-native';
const ANIMATION_DURATION = 500; // you can add your own animation time in ms
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
image: {
width: 50,
height: 20,
},
});
const IMAGES = [
// add your images here
];
const micAnimation = () => {
const animation = useRef(new Animated.Value(0)).current;
const [imageIndex, setImageIndex] = useState(0);
useEffect(() => {
Animated.loop(
Animated.timing(animation, {
toValue: 1,
duration: ANIMATION_DURATION,
useNativeDriver: true,
}),
).start();
}, []);
animation.addListener(({ value }) => {
const newIndex = Math.floor(value * IMAGES.length);
setImageIndex(newIndex);
});
const imageSource = IMAGES[imageIndex];
return (
<View style={styles.container}>
<Animated.Image
style={[styles.image, { opacity: animation }]}
source={imageSource}
/>
</View>
);
};
export default micAnimation;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论