-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreact-native-aufit-camera.js
More file actions
62 lines (57 loc) · 1.75 KB
/
react-native-aufit-camera.js
File metadata and controls
62 lines (57 loc) · 1.75 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import * as React from 'react';
import { StyleSheet, View } from 'react-native';
export default class FillToAspectRatio extends React.Component {
static defaultProps = {
ratio: '4:3',
};
state = {
layoutInfo: null,
};
handleLayout = ({ nativeEvent: { layout } }) => {
const { width, height } = layout;
this.setState({
layoutInfo: { width, height },
});
};
getRatio = () => {
const { ratio } = this.props;
const [ratioWidth, ratioHeight] = ratio.split(':').map(x => Number(x));
return ratioHeight / ratioWidth;
};
render() {
const { layoutInfo } = this.state;
if (!layoutInfo) {
return <View key="pre-info" onLayout={this.handleLayout} style={styles.containerStyle} />;
}
const { height, width } = layoutInfo;
let wrapperWidth;
let wrapperHeight;
const ratio = this.getRatio();
if (ratio * height < width) {
wrapperHeight = width / ratio;
wrapperWidth = width;
} else {
wrapperWidth = ratio * height;
wrapperHeight = height;
}
const wrapperPaddingX = (width - wrapperWidth) / 2;
const wrapperPaddingY = (height - wrapperHeight) / 2;
return (
<View onLayout={this.handleLayout} style={styles.containerStyle}>
<View
style={{
width: wrapperWidth,
height: wrapperHeight,
marginLeft: wrapperPaddingX,
marginTop: wrapperPaddingY,
}}
>
{this.props.children}
</View>
</View>
);
}
}
const styles = StyleSheet.create({
containerStyle: { flex: 1, overflow: 'hidden', position: 'relative' },
});