React native ImageBackground size, image not centered on smaller devices - react-native

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',
},

Related

react native, how to make a limited view by another view?

I have this problem and I have not been able to solve it, how can I make view 1 limited by view 2?
I need view 1 not to leave the edges of view 2, the image is on the outside, it should be cropped limited by view 2, I don't know if I make myself understood
<View style={styles.container}>
<View style={styles.progressBar}>
<Animated.View style={[styles.absoluteFill, { borderRadius: 30, backgroundColor: colors.blue2, width }]} />
</View>
<Text style={styles.progressText}>
{`${progress}%`}
</Text>
</View>
const styles = StyleSheet.create({
absoluteFill: {
position: 'absolute',
left: 0,
right: 0,
top: 0,
bottom: 0,
},
progressText: {
position: 'absolute',
color: colors.white,
fontFamily: "OpenSans-Regular"
},
container: {
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
height: 30,
borderRadius: 30,
},
progressBar: {
//alignSelf: 'baseline',
height: 30,
width: '100%',
backgroundColor: colors.blue3,
borderRadius: 30
}
})
thank you
You need to add an overflow: hidden at your progressBar style.
progressBar: {
height: 30,
width: '100%',
overflow: 'hidden',
backgroundColor: colors.blue3,
borderRadius: 30
}
I did this live demo for you:
https://codesandbox.io/s/vibrant-fermi-49ztl?file=/src/App.js

i want to decrease height but its not decreasing in react-native

I have created a component VeiwImageScreen and i imported in App.js I want this picture in mid of the screen that the two div above it can clearly be seen I have given width 80% its working height do not
I want to decrease its height and align it in center I have given align-items center but due to height it not coming in middle here is my code
<View style={styles.Container}>
<View style={styles.closeIcon}></View>
<View style={styles.deleteIcon}></View>
<View></View>
<Image
style={styles.VeiwImage}
source={require("../assets/horror-background.jpg")}
/>
</View>
this is my styling I want this picture in mid of the screen I have given width 80% and height 80%
const styles = StyleSheet.create({
Container: {
justifyContent: "center",
alignItems: "center",
height: "100%",
width: "100%",
},
VeiwImage: {
flex: 1,
width: "80%",
height: 10,
},
closeIcon: {
width: 50,
height: 50,
backgroundColor: colors.red,
position: "absolute",
zIndex: 1,
top: 20,
left: 20,
},
deleteIcon: {
zIndex: 1,
width: 50,
height: 50,
backgroundColor: colors.black,
position: "absolute",
top: 20,
right: 20,
},
});
Remove flex:1 from VeiwImage and add it to container
const styles = StyleSheet.create({
Container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
height: "100%",
width: "100%",
},
VeiwImage: {
width: "80%",
height: 10,
},
closeIcon: {
width: 50,
height: 50,
backgroundColor: colors.red,
position: "absolute",
zIndex: 1,
top: 20,
left: 20,
},
deleteIcon: {
zIndex: 1,
width: 50,
height: 50,
backgroundColor: colors.black,
position: "absolute",
top: 20,
right: 20,
},
});

Flex Layout with React Native

I'm having an issue where my container is collapsing on my child components which consists of a list of 4 images and one icon at the end of the container in a row. If I set the height explicitly then I can make it large enough to fit the content for one device. However, I have the images resizing themselves right now based off of width of the phone and a percent and then scaling to conserve aspect ratio. Is there a way to make my container grow just large enough to ensure it fits its child components when its child components scale based off of device width?
I thought that React Native flex default was to do this but for whatever reason my container is not fitting the images inside it but instead shrinking and all inside content is shrinking down except the images which lay over the top of my now shrunk containers.
export default function BadgeQuickView() {
return (
<View style={styles.badgeViewContainer}>
<View style={styles.badgeContainerTop}>
<View style={styles.badgeContainerLeftBorder}></View>
<Text style={styles.badgeContainerTitle}>FEATURED BADGES</Text>
<View style={styles.badgeContainerRightBorder}></View>
</View>
<View style={styles.quickBadgeList}>
{TEST_BADGE_ARRAY.map(option => (
<View style={styles.badgeInfoContainer} key={option.id}>
<Image style={styles.badgeImage} source={{uri: option.url}} fadeDuration={0} />
<Text style={styles.badgeDescText}>{option.name}</Text>
</View>
))}
<View style={styles.moreBadgesButtonContainer}>
<TouchableOpacity style={styles.moreBadgesButton}>
<Text style={styles.moreBadgesText}>All {`\n`} Badges</Text>
<Entypo name={'dots-three-horizontal'} size={moderateScale(20)} color={profileColors.editProfileText}/>
</TouchableOpacity>
</View>
</View>
</View>
);
}
const styles = StyleSheet.create({
badgeViewContainer: {
width: '100%',
height: moderateScale(130),
borderStyle: 'solid',
borderColor: userInterface.borderColor,
borderBottomWidth: 2,
backgroundColor: 'white',
paddingBottom: 5
},
moreBadgesText: {
color: 'black',
fontFamily: 'montserrat',
fontSize: moderateScale(8),
textAlign: 'center',
color: profileColors.editProfileText,
textAlign: 'center',
},
badgeDescText: {
color: 'black',
fontFamily: 'montserrat',
fontSize: moderateScale(8),
textAlign: 'center',
color: profileColors.editProfileText,
textAlign: 'center',
},
quickBadgeList: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-around',
paddingLeft: 15,
},
badgeImage: {
aspectRatio: 1,
width: '100%',
height: undefined,
resizeMode: 'contain',
marginBottom: 10,
},
moreBadgesButton: {
height: '100%',
width: '100%',
alignItems: 'center',
justifyContent: 'center',
textAlign: 'center'
},
moreBadgesButtonContainer: {
height: '100%',
width: '15%',
},
badgeInfoContainer: {
height: '100%',
width: '20%',
flexDirection: 'column',
padding: 5,
},
badgeContainerTop: {
flexDirection: 'row',
alignItems: 'center',
},
badgeContainerLeftBorder: {
borderStyle: 'solid',
borderColor: userInterface.borderColor,
borderTopWidth: 2,
width: '5%',
bottom: moderateScale(13) / 2,
},
badgeContainerTitle: {
fontFamily: 'montserrat-bold',
color: profileColors.editProfileText,
marginHorizontal: 10,
fontSize: moderateScale(13),
bottom: moderateScale(13) /2,
},
badgeContainerRightBorder: {
borderStyle: 'solid',
borderColor: userInterface.borderColor,
borderTopWidth: 2,
width: '100%',
bottom: moderateScale(13) / 2,
},
});
** Edit: Added current working code for reference. moderateScale(...) is just taking the screenHeight and scaling the height and working for most devices that I have tested thus far. I would like to not have to set this explicitly though if possible and instead have it expand based on its content.
Below is the desired and undesired looks. When I comment out the height: from the view container and set it to flex it won't expand to fit its content. When real images are added in the images show over the top of the bottom border and the text is not visible at all that will show under the red box typically. Added the red just as an indicator of where the image and text renders.

Blur the Half of the image in react-native

i am using this module for displaying blur image. My question is blur is applied to bottom of the image. This issue is faced in Android but iOS working fine.
Please follow this issue in GitHub:- enter link description here
Here is the screenshots:-
Android:-
iOS:-
<View style={styles.container}>
<Text>Hi, I am some unblurred text</Text>
<Image source={{ uri: 'https://picsum.photos/200/300' }} style={styles.img} />
<BlurView style={styles.blur} blurType="light" blurAmount={10} />
</View>
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-end',
alignItems: 'center',
position: 'relative',
zIndex: 1
},
img: {
flex: 1,
position: 'relative',
height: '100%',
zIndex: 2,
width: '100%'
},
blur: {
position: 'absolute',
left: 0,
bottom: 0,
right: 0,
height: '30%',
zIndex: 3
}
});
Hi, Just a case of styling each of the elements, in this case, its 30% high, but you can alter that figure on your project to be 50% or whatever you need it to be.
Hope this helps.

How to set `ImageBackground` image position in react native. Like in css `background-postion: bottom`?

So I'm setting a background image in react native but I need the position of the background image to be set to bottom. So if anyone knows how to achieve this.
I have tried adding backgroundPosition but it seems it is not supported
<ImageBackground
source={require("../assets/img/bg-top.png")}
// resizeMethod={'auto'}
style={{
width: "100%",
height: 120,
padding: 20,
paddingVertical: 40,
backgroundPosition: "bottom" // not working
}}
imageStyle={{
resizeMode: "cover",
alignSelf: "flex-end"
}}
>
<Text style={{ color: "#D8D8D8", fontSize: 16 }}>Login</Text>
</ImageBackground>;
I expect the image alignment should start from the bottom rather it starts from the top
React native provided bottom and position tag to set UI at bottom. Please replace backgroundPosition tag from image style into
position: 'absolute',
bottom:0
<ImageBackground
// resizeMethod={'auto'}
style={{
width: "100%",
height: 120,
backgroundColor:'#000',
padding: 20,
paddingVertical: 40,
position: 'absolute',
bottom:0
}}
imageStyle={{
resizeMode: "cover",
alignSelf: "flex-end"
}}
>
<Text style={{ color: "#D8D8D8", fontSize: 16 }}>Login</Text>
</ImageBackground>
Please check snack expo
https://snack.expo.io/#vishal7008/bottom-ui
The image inside ImageBackground has the default style:
position: 'absolute'
top: 0
bottom: 0
right: 0
left: 0
So, we can just remove the top: 0 by setting
top: undefined
together we have
<ImageBackground
source={require("../assets/img/bg-top.png")}
// resizeMethod={'auto'}
style={{
width: "100%",
height: 120,
padding: 20,
paddingVertical: 40,
overflow: 'hidden' // prevent image overflow the container
}}
imageStyle={{
resizeMode: "cover",
height: 200, // the image height
top: undefined
}}
>
<Text style={{ color: "#D8D8D8", fontSize: 16 }}>Login</Text>
</ImageBackground>
For implementing the position of the background image I use the following code, actually I use the ImageBackground component as the wrapper of the entire screen:
<ImageBackground
style={styles.screenWrapper}
imageStyle={styles.backgroundStyle}
source={{ uri: movieDetail?.image?.medium?.url }}
>
// children
~~~
const styles = createStyle({
screenWrapper: {
flex: 1,
position: 'relative',
},
backgroundStyle: {
resizeMode: 'cover',
position: 'absolute',
top: 0,
bottom: '45%',
},
Finally, I reach my desire:
Note: The above codes are not workable, it is just pseudo-code to convey my exact idea.
the backgroundPosition you know from CSS is not supported according the docs.
There is a similar question:
https://stackoverflow.com/a/53726116/9899193
Does this help you?