Needs audio progress bar like soundcloud react-native - reactjs

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.

Related

React Native: Why is image stretching vertically to fill container and how to prevent it?

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>
);
};

Same style for image , but different border radius on Android

The code
import { Image } from 'react-native';
<Image style={StyleSheet.flatten([styles.introPicSize, styles.introPicBorder])} source={require("../../assets/images/intro-pic.png")} />
const styles = StyleSheet.create({
introPicSize: {
height: responsiveWidth(160),
width: responsiveWidth(160),
resizeMode:'contain'
},
introPicBorder: {
borderWidth:8,
borderRadius:100,
borderColor:"#BDB9CD",
overflow:'hidden'
}
}
What expected is to have image with border radius 100 (circle) (On Android & IOS)
The result is fine for IOS
but for Android , it's a square not circle (it reads the borderRadius as 0 not 100
check the below image
The image
borderRadius does not work with <Image /> in Android. You can see it here: https://github.com/facebook/react-native/issues/8885
The solution is to wrap it with the View and apply borderRadius for it.
<View style = {{flex: 1, borderRadius: 100}} >
<Image style={ {width: 300, height: 100 } } source={{ uri: 'https://placekitten.com/300/100' }}> </Image>
</View>

How to display specific part of image in square

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>

Setting minimum image resolution in react-cropper

I am using react-cropper plugin for resizing the images. I need to provide minimum width and height below which the image cannot be resized. Also minimum width and height must be relative to the actual image size and not according to the viewport image size.
<Cropper
style={{
height: 422,
width: "90%",
margin: "0 auto",
marginTop: 25
}}
preview=".img-preview"
guides={false}
aspectRatio={16 / 9}
src={this.state.imageSrc}
ref="cropper"
viewMode={1}
className={classes.CropperCustomize}
zoomable={false}
/>
<Cropper
style={{
height: 422,
width: "90%",
margin: "0 auto",
marginTop: 25
}}
crop={this.crop}
draggable={false}
preview=".img-preview"
guides={false}
aspectRatio={16 / 9}
src={this.state.imageSrc}
ref="cropper"
viewMode={1}
className={classes.CropperCustomize}
zoomable={false}
/>
crop = e => {
if (
e.detail.width < this.props.cropWidth ||
e.detail.height < this.props.cropHeight
) {
this.refs.cropper.cropper.setData(
Object.assign({}, e.detail, {
width:
e.detail.width < this.props.cropWidth
? this.props.cropWidth
: e.detail.width,
height:
e.detail.height < this.props.cropHeight
? this.props.cropHeight
: e.detail.height
})
);
}
return;
};

How do I position a button at the bottom always? This will be rendered from a component whose parent is a scrollview

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

Resources