Image aspect ratio is used to set image scaling according to a given ration count. There are several type of image aspect ratio available for scaling image. In react native the Image component dose not support scale image according to width or height. But using the react-native-scalable-image custom component we can easily make the image component scalable. The react-native-scalable-image dose support all the major props like width, height and style. If we pass width only in react-native-scalable-image component then it will automatically resize the image according to given width and set its height. So in this tutorial we would learn about React Native Scalable Image Component Android iOS Example.
Contents in this project React Native Scalable Image – Android iOS Example:
1. First we need to download and install the react-native-scalable-image component in our react native project. So open your react native project root director and execute below command to install it.
1
|
npm install react–native–scalable–image —save
|
Screenshot of CMD:
Screenshot of CMD after finish installation:
2. Now open your project’s main App.js file and import View, StyleSheet and Text component.
1
2
3
|
import React from ‘react’;
import { View, StyleSheet, Text } from ‘react-native’;
|
3. Importing Image component from react-native-scalable-image library.
1
|
import Image from ‘react-native-scalable-image’;
|
4. Creating our main export default functional component App. This is our main parent component.
1
2
3
4
5
|
export default function App() {
}
|
5. Creating return() block and here we would first make a Root View component then make the Image component. We are calling the image from online URL, so if anyone copies my code it will directly run into your project.
List of React Native Scalable Image Props:
- width : Image width.
- height : Image height.
- onPress : To set click event on Image.
- background : Image can be used as background, Support Boolean True False value. Value should bet set True if user wants to set image as background.
- onSize : Is called with { width, height } as the first argument once image size is calculated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
return (
<View style={styleSheet.MainContainer}>
<Text style={{fontSize: 24}}> React Native Scalable Image </Text>
<Image
width={300}
// height={400}
// Image height will be automatically calculated.
// If user give specific Height then Width will be automatically calculated.
source={{ uri: ‘/wp-content/uploads/2021/01/cute_puppy.jpg’ }}
style={{marginTop: 20}}
/>
</View>
);
|
6. Creating Style.
1
2
3
4
5
6
7
8
9
10
|
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
padding: 12
},
});
|
7. Complete Source Code for App.js file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import React from ‘react’;
import { View, StyleSheet, Text } from ‘react-native’;
import Image from ‘react-native-scalable-image’;
export default function App() {
return (
<View style={styleSheet.MainContainer}>
<Text style={{fontSize: 24}}> React Native Scalable Image </Text>
<Image
width={300}
// height={400}
// Image height will be automatically calculated.
// If user give specific Height then Width will be automatically calculated.
source={{ uri: ‘/wp-content/uploads/2021/01/cute_puppy.jpg’ }}
style={{marginTop: 20}}
/>
</View>
);
}
const styleSheet = StyleSheet.create({
MainContainer: {
flex: 1,
justifyContent: ‘center’,
alignItems: ‘center’,
padding: 12
},
});
|
Screenshot: