React Native rounded image with a border - react-native

I want to create a rounded image with a border. If I add borderColor: 'green', borderWidth:1, border is visible only in top left part of the rounded image.
<TouchableHighlight
style={[styles.profileImgContainer, { borderColor: 'green', borderWidth:1 }]}
>
<Image source={{ uri:"https://www.t-nation.com/system/publishing/articles/10005529/original/6-Reasons-You-Should-Never-Open-a-Gym.png" }} style={styles.profileImg} />
</TouchableHighlight>
export default styles = StyleSheet.create({
profileImgContainer: {
marginLeft: 8,
height: 80,
width: 80,
borderRadius: 40,
},
profileImg: {
height: 80,
width: 80,
borderRadius: 40,
},
});

overflow: 'hidden' for images container solves this issue.

Use the following styling, it's work for me.
image: {
width: 150,
height: 150,
borderRadius: 150 / 2,
overflow: "hidden",
borderWidth: 3,
borderColor: "red"
}

Worth to mention for Android...
I had to specifically set resizeMode="cover" for the borderRadius to take effect.
<Image
style={styles.image}
source={source}
resizeMode={"cover"} // <- needs to be "cover" for borderRadius to take effect on Android
/>
const styles = StyleSheet.create({
image: {
width: 150,
height: 150,
borderColor: 'red,
borderWidth: 2,
borderRadius: 75
},
});

Border width adds up to the size of the component that you added to. This makes your image bigger than the size of your container component. To solve this issue you can add the border width to the component sizes.
Example
const styles = StyleSheet.create({
profileImgContainer: {
marginLeft: 8,
height: 82,
width: 82,
borderRadius: 40,
borderWidth: 1
},
profileImg: {
height: 80,
width: 80,
borderRadius: 40,
},
});

If you want to show the image with rounded corner and with resizeMode={'contain'} to show the full image then wrap the image inside a view and then give border radius to the view. it will work
<View style={styles.carImageHolder}>
<Image
source={{
uri: each?.usercar_details?.car_model?.sized_photo,
}}
resizeMode={'contain'}
style={styles.carImage}
/>
</View>
and the style part
carImage: {
width: '100%',
aspectRatio: 1 / 1,
},
carImageHolder: {
width: '28.3%',
aspectRatio: 1 / 1,
backgroundColor: '#d8d8d8',
borderRadius: 25,
overflow: 'hidden',
},

The answers given here, are nice, but, from my experience, it's better to use percentages of your available screen height, as the width and height dimensions of image. This will help a lot with responsiveness. Do it like this
import RN from 'react-native';
const SCREEN_HEIGHT = RN.Dimensions.get('window').height;
Then apply the following as the dimensions styling, to get a nice, responsive rounded image.
style={[
//... your other previous styles
{
resizeMode: 'cover',
width: SCREEN_HEIGHT * 0.15,
height: SCREEN_HEIGHT * 0.15,
borderRadius: (SCREEN_HEIGHT * 0.15)/2,
}
]}
The (*0.15 i.e 15% of screen height) is just my choice of sample dimensions, you can use higher or lower, depending on how big you want your image to be.

Related

React Native: minHeight in percent is not working correctly like in Web

Below are my experimental result. Does those seems right?
minHeight in percent is not working correctly like in Web, is there any workaround about it?
#--- flexDirection = column; React Native Version = 0.68.4 ;
minHeight always cross height and maxHeight.
minWidth always cross width and maxWidth.
flex will not cross maxHeight.
alignSelf=stretch will not cross width.
flex/%height/%maxHeight only be used if height keyword present in container.
alignSelf=stretch/%minWidth/%width/%maxWidth only be used if width keyword present in container.
for primary axis(here column) %minHeight is not working correctly. need to use number value only.
for secondary axis (here row) all /%minWidth/%width/%maxWidth works.
---#
Thanks in advance.
N.B: Sample code
// I applied on every elements below styles
// margin: 0,
// padding: 0,
// borderWidth: 0,
// borderRadius: 0,
// left: 0,
// top: 0,
// backgroundColor: 'transparent',
// overflow: 'hidden',
<>
<View
style={{
width: 200,
height: 300,
backgroundColor: 'red',
}}
onLayout={(e) => {
console.log('Main: ', e.nativeEvent.layout);
}}
>
<View
style={{
//see image 1
flex: 1,
minWidth: 150,
width: '50%',
maxWidth: 120,
minHeight: 120,
height: '50%',
maxHeight: 192,
backgroundColor: 'green',
//removed previous code and changed to below. see image 2
// flex: 1,
minWidth: 150,
width: '50%',
maxWidth: 120,
// minHeight: 120,
minHeight: '40%',
height: '50%',
// maxHeight: 192,
backgroundColor: 'green',
}}
onLayout={(e) => {
console.log('AfterMain: ', e.nativeEvent.layout);
}}
>
</View>
</View>
</>
image 1
image 2

how do i set the border radius of the lottieview in a react native page

the style of the view. I've tried adding the height and width too
logo: {
marginRight: 40,
marginLeft: 40,
marginTop: 10,
backgroundColor: '#68a0cf',
borderRadius: 50,
borderWidth: 1,
borderColor: '#fff',
overflow: 'hidden'
},
For the Lottie view
<LottieView
style={styles.logo}
source={require('../assets/church.json')}
autoPlay
loop
/>
You can set a borderRadius to the container of the lottie View and just give it an overflow: 'hidden'
<View style={styles.lottieContainer}>
<LottieView
loop
autoPlay
style={styles.lottieView}
source={require('./scrolling-social-mdia.json')}/>
</View>
these are the styles:
lottieContainer: {
overflow: 'hidden',
borderRadius: 20,
},
lottieView: {
width: '100%'
}

React native ImageBackground size, image not centered on smaller devices

I am setting my background image, which stretches the full width of the screen. How can I scale the image for smaller devices and still keep it centered?
<ImageBackground
source={{ uri: `${backgroundImage?.url}?width=${windowWidth}` }}
style={styles.imageBackground}
imageStyle={styles.imageStyle}
>
imageBackground: {
width: '100%',
height: '100%',
},
imageStyle: {
resizeMode: 'contain',
width: '80%',
left: 20,
top: 40,
alignItems: 'center',
justifyContent: 'center',
display: windowWidth < 380 ? 'none' : 'flex',
},

Why does react-native border radius work on iOS but not Android Image

Created a list component, it has an image, some text, and a button.
the image has a border radius and borderColor on it.
Problem:
the colored-border-radius on android isn't being recognized, but on iOS it works fine...
here's the code:
List:
import React, { useState } from 'react';
import {
View,
Text,
Image,
StyleSheet,
Modal,
FlatList,
Dimensions,
TouchableOpacity,
TouchableWithoutFeedback
} from 'react-native';
import { Svg, Path, G, Line } from 'react-native-svg';
const { width } = Dimensions.get('window');
const BlockList = (props) => {
const onPress = (name) => {
alert('Unblocking ' + name);
};
return (
<FlatList
style={styles.container}
data={props.data}
renderItem={({ item, index }) => (
<View style={styles.itemContainer}>
<View style={styles.leftSide}>
<Image source={item.img} style={styles.img} resizeMode={'contain'} />
<Text style={{ fontSize: 18, fontWeight: 'bold', color: '#454A66' }}>{item.name}</Text>
</View>
<View style={styles.rightSide}>
<TouchableOpacity onPress={() => onPress(item.name)} style={styles.btn}>
<Text style={{ fontWeight: 'bold', color: '#fff' }}>Unblock</Text>
</TouchableOpacity>
</View>
</View>
)}
/>
);
};
const styles = StyleSheet.create({
itemContainer: {
flexDirection: 'row',
width: width * 0.95,
backgroundColor: '#fff',
marginHorizontal: width * 0.025,
marginBottom: width * 0.02,
borderRadius: 18,
justifyContent: 'space-between',
alignItems: 'center',
paddingVertical: width * 0.02,
shadowColor: '#333',
shadowOffset: {
width: 3,
height: 3
},
shadowOpacity: 0.5,
shadowRadius: 3,
elevation: 5
},
img: {
borderRadius: 50,
borderWidth: 2,
borderColor: '#219F75',
height: width * 0.125,
width: width * 0.125,
marginHorizontal: width * 0.05
},
btn: {
borderRadius: 11,
backgroundColor: '#219F75',
padding: width * 0.0275
},
leftSide: {
flexDirection: 'row',
alignItems: 'center'
},
rightSide: {
marginRight: width * 0.05
}
});
export default BlockList;
here's what its supposed to look like (working correctly on iOS):
here's what is looks like on android (notice the square green border):
Why is this happening and how can I get my border radius?
if we want the view circle, we also realize it by setting the borderRadius equals half of the view height(width).
<View
style={{
borderWidth:1,
borderColor:'rgba(0,0,0,0.2)',
alignItems:'center',
justifyContent:'center',
width:100,
height:100,
backgroundColor:'#fff',
borderRadius:50,
}}
/>
In your situation, your image sets the resizeMode. so it did not show the effect you want. for that try to move it in a view or use resizeMode:stretch; resizeMode:cover or remove it.
<View style={styles.leftSide}>
<Image source={item.img} style={styles.img}/>
<Text style={{ fontSize: 18, fontWeight: 'bold', color: '#454A66' }}>{item.name}</Text>
</View>
img: {
borderRadius: width * 0.125*0.5,
borderWidth: 2,
borderColor: '#219F75',
height: width * 0.125,
width: width * 0.125,
marginHorizontal: width * 0.05
},
contain: Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding).
Try borderRadius:width * 0.125 under img style.
You set image width height dynamically but border radius static value when using lower resolution device of android sometimes image borderRedius is larger to image width that's why it's occurred.
Please set all attribute dynamically. It will be better if use image borderRedius half of the width
try this in img style
borderRadius: (width * 0.125)/2
You can try to change the resizeMode to "cover"

React Native Aspect fit image

<Image
style={{
alignSelf: 'center',
flex: 1,
width: '25%',
height: null,
resizeMode: 'cover',
borderWidth: 1,
borderRadius: 75,
}}
source={{uri:'https://facebook.github.io/react/img/logo_og.png'}}
resizeMode="stretch"
/>
https://snack.expo.io/rJCoC9S5-
How would i make the image width 25% of the parent, and the height to whatever that is needed to maintain the original width:height aspect ratio?
You can edit the style of your image like this:
<Image style={style.image} source={require('.....')} />
const style = StyleSheet.create({
image: {
height: 80,
width: 80,
resizeMode: 'contain'
}
});
contain is what you are looking for. But there are more options like strech and cover.
Aspect Fill in iOS and Center Crop in Android by these lines of code.
image: {
flex: 1,
width: null,
height: null,
resizeMode: 'contain'
}