Sign Up

Have an account? Sign In Now

Sign In

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Sign InSign Up

Softans

Softans Logo Softans Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
Home/ Questions/Q 1857
In Process
Jesson Holder
Jesson Holder
Asked: June 10, 20222022-06-10T04:45:23+00:00 2022-06-10T04:45:23+00:00

how to make image show fade in and out with pure react native? (with function component)

thanks for ans! react native animated : why my loop image animation is worked like a weird?

i have successfully created App behavior i want. so i want to share this

we have to know this

  1. absolute position
  2. useref
  3. animated

first, save and declare images

const MainScreen = () => {
  const imageSources = [
    require('../assets/image/meadow.jpg'),
    require('../assets/image/mountain.jpg'),
    require('../assets/image/sea.jpg'),
    require('../assets/image/desert.jpg'),
  ]

  return (
    <View style={{ styles.yourLayout, }}>

      
   
      <YourComponents />
    </View>
  );
};



export default MainScreen;

second, make custom image component

import React, {useRef,useEffect} from "react";
import { Animated,  } from 'react-native';


interface BackgroundImageProps{
    delay: number,
    imageSource: any,
}

const CustomImage= ({delay, imageSource}:BackgroundImageProps) => {

    const fadeAni = useRef<Animated.Value>(new Animated.Value(0)).current;
    //opacity 1: visible, 0: invisible
  
    useEffect(() => {
        //make fadein <-> fadeout loop
    }, [fadeAni])


    return (
        <Animated.Image style={{
            opacity: fadeAni,
            position: 'absolute',
            left: 0,
            top: 0,
            width: '100%',
            height: '100%',
        }} source={imageSource} />
    );
}
/*

*/


export default BackgroundImage;

third, make animation
we want to start animation when as soon as component rendered, we use useEfecct
since other image to be invisible when other image visible, we design the animation sequence(delay,loop). so we can start each loop for each images.
when animated value is increased to 1,
show image 2secs
when animated value is decreased to 0,
wait for other image.

Animated.sequence([
            Animated.delay(delay),
            Animated.loop(
                Animated.sequence([
                    Animated.timing(fadeAni, {
                        toValue: 1,
                        duration: 1000,
                        useNativeDriver: true,
                    }),
                    Animated.delay(2000),
                    Animated.timing(fadeAni, {
                        toValue: 0,
                        duration: 1000,
                        useNativeDriver: true,
                    }),
                    Animated.delay(8000),
                ]),
            )
        ]).start()

we must add animated value dependency in useEffect. (like the code) if you want to know more click the url in my question.

apply

const MainScreen = () => {
  const imageSources = [
    require('../assets/image/meadow.jpg'),
    require('../assets/image/mountain.jpg'),
    require('../assets/image/sea.jpg'),
    require('../assets/image/desert.jpg'),
  ]

  return (
    <View style={{ styles.yourLayout, }}>
      <BackgroundImage imageSource={imageSources[0]} delay={0} />
      <BackgroundImage imageSource={imageSources[1]} delay={3000} />
      <BackgroundImage imageSource={imageSources[2]} delay={6000} />
      <BackgroundImage imageSource={imageSources[3]} delay={9000} />
      <YourComponents />
    </View>
  );
};

backgroundimage component’s position is ‘absolute’ so layout between yourcomponent and view is free from backgroundimage.

please advise about my mistake and better component!

  • 0
  • 1 1 Answer
  • 29 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

1 Answer

  • Voted
  • Oldest
  • Recent
  1. Jesson Holder
    2022-06-10T04:45:44+00:00Added an answer on June 10, 2022 at 4:45 am

    thanks for ans! react native animated : why my loop image animation is worked like a weird?

    i have successfully created App behavior i want. so i want to share this

    we have to know this

    1. absolute position
    2. useref
    3. animated

    first, save and declare images

    const MainScreen = () => {
      const imageSources = [
        require('../assets/image/meadow.jpg'),
        require('../assets/image/mountain.jpg'),
        require('../assets/image/sea.jpg'),
        require('../assets/image/desert.jpg'),
      ]
    
      return (
        <View style={{ styles.yourLayout, }}>
    
          
       
          <YourComponents />
        </View>
      );
    };
    
    
    
    export default MainScreen;
    

    second, make custom image component

    import React, {useRef,useEffect} from "react";
    import { Animated,  } from 'react-native';
    
    
    interface BackgroundImageProps{
        delay: number,
        imageSource: any,
    }
    
    const CustomImage= ({delay, imageSource}:BackgroundImageProps) => {
    
        const fadeAni = useRef<Animated.Value>(new Animated.Value(0)).current;
        //opacity 1: visible, 0: invisible
      
        useEffect(() => {
            //make fadein <-> fadeout loop
        }, [fadeAni])
    
    
        return (
            <Animated.Image style={{
                opacity: fadeAni,
                position: 'absolute',
                left: 0,
                top: 0,
                width: '100%',
                height: '100%',
            }} source={imageSource} />
        );
    }
    /*
    
    */
    
    
    export default BackgroundImage;
    

    third, make animation
    we want to start animation when as soon as component rendered, we use useEfecct
    since other image to be invisible when other image visible, we design the animation sequence(delay,loop). so we can start each loop for each images.
    when animated value is increased to 1,
    show image 2secs
    when animated value is decreased to 0,
    wait for other image.

    Animated.sequence([
                Animated.delay(delay),
                Animated.loop(
                    Animated.sequence([
                        Animated.timing(fadeAni, {
                            toValue: 1,
                            duration: 1000,
                            useNativeDriver: true,
                        }),
                        Animated.delay(2000),
                        Animated.timing(fadeAni, {
                            toValue: 0,
                            duration: 1000,
                            useNativeDriver: true,
                        }),
                        Animated.delay(8000),
                    ]),
                )
            ]).start()
    

    we must add animated value dependency in useEffect. (like the code) if you want to know more click the url in my question.

    apply

    const MainScreen = () => {
      const imageSources = [
        require('../assets/image/meadow.jpg'),
        require('../assets/image/mountain.jpg'),
        require('../assets/image/sea.jpg'),
        require('../assets/image/desert.jpg'),
      ]
    
      return (
        <View style={{ styles.yourLayout, }}>
          <BackgroundImage imageSource={imageSources[0]} delay={0} />
          <BackgroundImage imageSource={imageSources[1]} delay={3000} />
          <BackgroundImage imageSource={imageSources[2]} delay={6000} />
          <BackgroundImage imageSource={imageSources[3]} delay={9000} />
          <YourComponents />
        </View>
      );
    };
    

    backgroundimage component’s position is ‘absolute’ so layout between yourcomponent and view is free from backgroundimage.

    please advise about my mistake and better component!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • Popular
  • Answers
  • Ghulam Nabi

    Why are the British confused about us calling bread rolls ...

    • 5 Answers
  • Alex

    application has failed to start because no appropriate graphics hardware ...

    • 4 Answers
  • Jerry

    Add file to native target programmatically via tuist/XcodeProj

    • 4 Answers
  • Ghulam Nabi
    Ghulam Nabi added an answer To resolve the NullPointerException, you need to identify the variable… March 15, 2023 at 8:25 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can replace the PnP code in your Azure Function… February 13, 2023 at 7:11 am
  • Ghulam Nabi
    Ghulam Nabi added an answer You can use the $match stage in the aggregate pipeline… February 10, 2023 at 6:20 am

Trending Tags

android c++ cypress flutter java javascript python selenium testng webdriver

Top Members

Robert

Robert

  • 3 Questions
  • 1k Points
Luci

Luci

  • 5 Questions
  • 1k Points
Kevin O Brien

Kevin O Brien

  • 2 Questions
  • 1k Points

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Softans

Softans is a social questions & Answers Engine which will help you establish your community and connect with other people.

About Us

  • Blog
  • Jobs
  • About Us
  • Meet The Team
  • Contact Us

Legal Stuff

Help

Follow

© 2021 Softans. All Rights Reserved
With Love by Softans.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.