I have problem with Image component in react native. I would like to display specific part of image in some square. F.e:
Let's say, I have image in resolution: 1280x720
private someImage = require('../Assets/someImage.jpg');
I would like to display this image in square component (with constant size)
<View style={{width: 64, height: 64}}>
<Image source={this.someImage}
style={{position: 'absolute', top: 0, left: 0 }} />
</View>
Here comes the first problem: Image goes beyond the square. How can I show only part of my image (but with original resolution).
The second question is:
Is there a way to showing specific fragment of my image? That's men if I set top: -64, left: -64 I would like to see original image move about 64 pixels diagonally.
Jroslaw. this may helpful for you.
const someImage = require('../Assets/someImage.jpg');
class ImageClip extends Components {
...
render() {
return (
...
<View style={{ width: 64, height: 64, overflow: 'hidden' }}> // width and height of image you want display.
<Image
source={someImage}
style={{
top: 24,
left: 32,
}} //position of image you want display.
/>
</View>
...
);
}
}
To show your image with full resolution but with only the section you choose, you will need to display the image within a fixed sized container. This image will also need to be a background-image in the style attribute.
<View style={{background-image: this.someImage, background-position-x: -64px, background-position-y: -64px}}>
Giving " overflow:'hidden' " to the parent view also works for me.
Resize-mode:contain will NOT work, as it will resize your image to fit entirely within the 64x64 container, not what you want in this case.
<View style={{width: 64, height: 64, overflow: 'hidden'}}>
<Image
source={{uri: 'https://picsum.photos/200/300'}}
style={{
height: 200,
width: 200,
top: -50,
left: -70,
}}
/>
</View>
Related
I have the following image in my React Native app
I want to have this image inside a parent element with no space above or below it, and I want the image to not overshoot the left or right sides of the screen
When I have the code like this
<View style={{
marginTop: 100,
borderWidth: 2,
borderColor: 'red'
}}>
<Image
style={{
width: '100%'
}}
source={require('#images/swoosh-02.png')}
/>
</View>
I get this, because default for resizeMode is cover
When I change it to this (adding resizeMode='contain')
<View style={{
marginTop: 100,
borderWidth: 2,
borderColor: 'red'
}}>
<Image
style={{
width: '100%'
}}
resizeMode='contain'
source={require('#images/swoosh-02.png')}
/>
</View>
the image gets successfully contained horizontally, but the height of the parent element remains the same as it was when resizeMode was cover.
How can I get it so that the parent element shrinks vertically to contain only the image and not have that extra top/bottom padding?
De diference between contain and cover is :
resizeMode='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).
resizeMode='cover' = 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 larger than the corresponding dimension of the view (minus padding).
Maybe you should try to apply height in your View.
Read this article, should help you.
I'm relatively new to React and I'm trying to create a sort of dashboard for Cryptocurrency prices.
I'm using react-financial-charts to generate an OHLCV chart within a ResponsiveReactGridLayout.
However, the svg always displays outside the grid. Can anyone help with how I could fix this:
Here is the code: https://github.com/astronights/market_analysis_tool_evaluation-ui/blob/master/src/components/Home/Home.tsx where I render my Component that contains the ChartCanvas.
<div key="candlestick" className="home-card">
<div ref={canvasRef} style={{ width: "100%", height: "100%" }}>
{canvasRef.current ? (
<Candlestick
coin={coin}
width={canvasRef.current.clientWidth}
height={canvasRef.current.offsetHeight}
/>
) : null}
</div>
</div>
and here is the code for the chartCanvas: https://github.com/astronights/market_analysis_tool_evaluation-ui/blob/master/src/components/Home/Candlestick.tsx
return (
<ChartCanvas
height={props.height}
ratio={1}
width={props.width}
margin={{ bottom: 50, top: 100, left: 50, right: 50 }}
padding={0}
seriesName={`OHLCV prices - ${props.coin.coin}`}
data={prices}
xScale={scaleTime()}
xAccessor={xAccessor}
xExtents={xExtents}
/>
As you can see in the image, the SVG chart generated seems to be going outside.
Fixed by adding these lines of CSS.
.react-financial-charts {
position: absolute !important;
top: 0;
left: 0;
}
canvas {
position: absolute !important;
top: 0;
left: 0;
}
I needs progress bar like soundcloud (Pointer fixed in center) Like This
I tried with React Native Slider but unable fixed in center
function TrackProgres() {
const bufferedPosition = 0
const duration = 600
return (
<ScrollView horizontal={true}>
<ImageBackground source={require('../../../../assets/images/sound.png')} style={{width:550, height: 40,zIndex:-1,left: 50,marginRight: -50 }} imageStyle={{resizeMode: 'repeat'}}>
<Slider
style={{width: 550}}
value={bufferedPosition}
maximumValue={duration}
minimumTrackTintColor="#292b2c"
maximumTrackTintColor="#292b2c"
thumbStyle={styles.thumb}
trackStyle={styles.track}
onValueChange={(value) => seekTo(value)}
/>
</ImageBackground>
</ScrollView>
)
What you're looking for isn't a slider - it's just a conditional rendering on a background position. Think of it like this:
const { width } = Dimensions.get('window');
return (
<View
style={{
position: 'absolute',
height: 30,
width,
top: 0,
left: (width / 2) - (width * percentComplete),
backgroundColor: '#F00',
}}
>
<View
style={{
position: 'absolute',
backgroundColor: '#FFF',
top: 0,
left: (width / 2) - 1,
width: 2,
height: 30,
}}
/>
</View>
);
If you render these with percentComplete as a value between 0 and 1, the background View will be offset properly.
At 0% complete, the View will start at left: width / 2, or the center of the screen. As percentComplete increases, that offset will decrease, and go beyond the left edge of the screen after 50% complete. At 100%, the right edge of the background View will be at the midpoint of the screen.
The foreground View remains in the center.
In my React Native app, I have a red box of height 300 that I want to center vertically, and an image that I want to sit inside and at the top of this red box. Here is my code so far:
import React, { Component } from 'react';
import { View, Image} from 'react-native';
export default class Login extends Component {
render() {
return (
<View style={{
flex: 1,
justifyContent: "center",
alignItems: "center"
}}>
<View
style={{
borderWidth: 3,
borderColor: 'red',
width: "100%",
height: 300
}}
>
<Image
source={require('../../images/swoord-top.png')}
style={{
width: "100%",
height: "100%",
resizeMode: "contain",
borderWidth: 3
}}
>
</Image>
</View>
</View>
);
}
}
Here's what this looks like:
The red box is the outer box I mentioned, and the black box is just the border of the Image. The problem is that this black box (ie the Image) expands to fill the red box vertically, and the image is vertically centered inside this black box, so it's vertically centered inside the red box, as opposed to at the flex-start position that I want it at. I've tried adding justifyContent: flex-start and flexShrink: 1 to the Image and neither has made a difference.
Does anyone know how I can approach this?
NOTE:
If I remove the height: 100% on the Image, I get this:
UPDATE:
To clarify, this is what I'd like it to look like. I've removed the black border here. I moved it up to where I want it by adding top: -95, but this won't work in general because the value would be different for different devices:
try doing like this
<Image
source={require('../../images/swoord-top.png')}
style={{
width: "100%",
borderWidth: 3
}}
resizeMode={"contain"}
/>
There is a difference between Image (the view) and the content of that image. On your screenshot, the image view has the black border and its content is sitting inside of it. Because they have different aspect ratios, there is going to be either a blank space or cut-off on the content. You can't adjust position of the content as it's not laid out on flex basis. Image view is just a window that then renders image content. There is no property to tell Image where to align that content
Your problem comes from the fact that screen width differs, and aspect ratio of your container also differs (variable width but constant 300 height). What you need to do is
measure container width
determine proper aspect ratio for the image based on image resource width and height
set your image width to 100% and its height according to received aspect ratio
here, my image dimensions are 1005 * 219:
const Test = () => {
const [width, setWidth] = useState(0);
return (
<View>
<View
onLayout={e => setWidth(e.nativeEvent.layout.width)}
style={{ width: '100%', height: 300, borderWidth: 1 }}>
<Image
style={{
width: '100%',
height: (width / 1005) * 219,
borderWidth: 1,
borderColor: 'red',
}}
source={require('...')}
/>
</View>
</View>
);
};
How do I position a button at the bottom always? This will be rendered from a component whose parent is a scrollview. When I use abosolute position, positioning works, but when I scroll the view the button scrolls as well.
positionInBottom {
position: 'relative',
width: 50,
height: 50,
top: 160 - 26,
left: Dimensions.get('window').width - 70,
backgroundColor: 'red,
zIndex: 100,
}
Here is my componet structure: It's a sudo code.
<Page>
<ScrollView>
<Items items={this.items}/>
</ScrollView>
</Page>
<Items>
{items.map((item) => {
<Text>{item.name}</Text>
})
}
<Button stype={styles.positionInBottom} name="stay in the bottom" label="stay in the bottom"/>
</Items>
The following the documentation from react native. What does the bolded lines exactly means? I don't understand it.
position ReactPropTypes.oneOf([ 'absolute', 'relative' ])
position in React Native is similar to regular CSS, but everything is set to relative by default, so absolute positioning is always just relative to the parent.
If you want to position a child using specific numbers of logical pixels relative to its parent, set the child to have absolute position.
If you want to position a child relative to something that is not its parent, just don't use styles for that. Use the component tree.
See https://github.com/facebook/css-layout for more details on how position differs between React Native and CSS.
positionInBottom {
position: 'absolute',
width: 50,
height: 50,
bottom: 0,
left: Dimensions.get('window').width - 70,
backgroundColor: 'red',
zIndex: 100,
}