그라데이션이란 어떤 색조, 명암, 질감을 단계적으로 다른 색조, 명암, 질감으로 바꾸는 예술 기법을 의미한다
react native에서 그라데이션을 구현하는 방법에는 여러 방법이 있지만 expo프로젝트를 진행중인 만큼 expo에서 사용할 수 있는 방법에 대해 설명할것이다.
expo를 사용중이면 그라데이션 효과를 넣는게 매우 간단한데, 바로 expo-linear-gradient의 LinearGradient component를 사용하면 된다.
npx expo install expo-linear-gradient
먼저 expo-linear-gradient 모듈을 받아온 뒤, LinearGradient를 import해주면 된다.
import { LinearGradient } from "expo-linear-gradient";
<LinearGradient colors={["#4e0429", "#ddb52f"]} style={styles.rootScreen}>
<StartGameScreen />
</LinearGradient>
해당 컴포넌트는 특별한 property를 가진다.
더 자세한 property들은 공식 문서를 참고하자 https://docs.expo.dev/versions/latest/sdk/linear-gradient/#end
react native에서는 배경 이미지를 추가할 때 내장 컴포넌트인 ImageBackground component를 사용하면 된다.
사용법도 매우 간단한데,
<LinearGradient colors={["#4e0429", "#ddb52f"]} style={styles.rootScreen}>
<ImageBackground
style={styles.rootScreen}
imageStyle={styles.backgroundImage}
source={require("./assets/images/background.png")}
>
<StartGameScreen />
</ImageBackground>
</LinearGradient>
이전에 이미지를 넣을때와 동일하게 이미지의 경로를 받아올때는 require함수를 통해 현재 파일 기준 상대경로를 작성해야한다.
ImageBackground 컴포넌트는 Image Props를 상속받고 있다.
위 코드를 보면 imageStyle이라는 property가 보이는데, 해당 property를 통해서도 style을 지정할 수 있다.
더 자세한 사항은 공식문서를 참고하자 https://reactnative.dev/docs/imagebackground