循环多个图像以在React Native中创建动画

huangapple go评论50阅读模式
英文:

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(&#39;..//../img/ic_1.png&#39;),
  require(&#39;..//../img/ic_2.png&#39;),
  require(&#39;..//../img/ic_3.png&#39;),
  require(&#39;..//../img/ic_4.png&#39;),
  require(&#39;..//../img/ic_5.png&#39;),
  require(&#39;..//../img/ic_6.png&#39;),
];
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.

        &lt;&gt;
       {animImages.map(img =&gt; &lt;Image source={img} /&gt;)}
     &lt;/&gt; 

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 ? 
        &lt;View style={styles.elevatedView}&gt;
        &lt;TouchableOpacity
        onPress={() =&gt; {
          console.log(&quot;button pressed&quot;);
          // stop the image loop 
        }} 
        &gt;
          {The loop for images comes here }
        &lt;/TouchableOpacity&gt;
        &lt;/View&gt;
        : 
        &lt;View style={styles.elevatedView}&gt;
          &lt;IconsRender
            icon={acomponentProps.icons.icon}
          /&gt;
        &lt;/View&gt;
        }

答案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)

Example:


import React from &#39;react&#39;;
import { StyleSheet, View, Dimensions, Button } from &#39;react-native&#39;;

import FadingSlides from &#39;./FadeImage/fadeimage&#39;;

const { width, height } = Dimensions.get(&#39;window&#39;);

const slides = [
  {
    image: require(&#39;./assets/snack-icon.png&#39;),
    imageWidth: width - width * 0.3,
    imageHeight: width - width * 0.3,
    title: &#39;JavaScript&#39;,
    subtitle: &#39;The definitive language&#39;,
    titleColor: &#39;#fff&#39;,
    subtitleColor: &#39;yellow&#39;,
  },
  {
    image: require(&#39;./assets/snack-icon.png&#39;),
    imageWidth: width - width * 0.3,
    imageHeight: width - width * 0.3,
    title: &#39;ReactJS&#39;,
    subtitle: &#39;JavaScript coolest library&#39;,
    titleColor: &#39;#fff&#39;,
    subtitleColor: &#39;cyan&#39;,
  },
];

export default App = () =&gt; {
  const [startAnimation, setStartAnimation] = React.useState(true);

  return (
    &lt;View style={styles.container}&gt;
      &lt;FadingSlides
        slides={slides}
        fadeDuration={1200}
        stillDuration={2000}
        startAnimation={startAnimation}
        height={height}
      /&gt;
      &lt;Button
        title=&quot;Toggle animation&quot;
        onPress={() =&gt; {
          setStartAnimation(() =&gt; {
            return !startAnimation;
          });
        }}
      /&gt;
    &lt;/View&gt;
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: &#39;center&#39;,
    alignItems: &#39;center&#39;,
    backgroundColor: &#39;#ff0000&#39;,
  },
});


答案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 &#39;react&#39;;
import { Animated, Image, StyleSheet, View , Text} from &#39;react-native&#39;;
const ANIMATION_DURATION = 500; // you can add your own animation time in ms
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: &#39;center&#39;,
alignItems: &#39;center&#39;,
},
image: {
width: 50,
height: 20,
},
});
const IMAGES = [
// add your images here 
];
const micAnimation = () =&gt; {
const animation = useRef(new Animated.Value(0)).current;
const [imageIndex, setImageIndex] = useState(0);
useEffect(() =&gt; {
Animated.loop(
Animated.timing(animation, {
toValue: 1,
duration: ANIMATION_DURATION,
useNativeDriver: true,
}),
).start();
}, []);
animation.addListener(({ value }) =&gt; {
const newIndex = Math.floor(value * IMAGES.length);
setImageIndex(newIndex);
});
const imageSource = IMAGES[imageIndex];
return (
&lt;View style={styles.container}&gt;
&lt;Animated.Image
style={[styles.image, { opacity: animation }]}
source={imageSource}
/&gt;
&lt;/View&gt;
);
};
export default micAnimation;

huangapple
  • 本文由 发表于 2023年4月7日 04:56:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定