-
Notifications
You must be signed in to change notification settings - Fork 13
/
Resolution.js
81 lines (70 loc) · 2.24 KB
/
Resolution.js
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, {Component, PropTypes} from 'react';
import {
Dimensions,
PixelRatio,
Platform,
StatusBar,
View
} from 'react-native';
let props = {};
export default class Resolution {
static get(useFixWidth = true){
return useFixWidth?{...props.fw}:{...props.fh}
}
static setDesignSize(dwidth=750,dheight=1336,dim="window"){
let designSize = {width:dwidth,height:dheight};
let navHeight = Platform.OS === 'android' ? StatusBar.currentHeight : 0;
let pxRatio = PixelRatio.get(dim);
let {width,height} = Dimensions.get(dim);
if(dim != "screen")height-=navHeight;
let w = PixelRatio.getPixelSizeForLayoutSize(width);
let h = PixelRatio.getPixelSizeForLayoutSize(height);
let fw_design_scale = designSize.width/w;
fw_width = designSize.width;
fw_height = h*fw_design_scale;
fw_scale = 1/pxRatio/fw_design_scale;
let fh_design_scale = designSize.height/h;
fh_width = w*fh_design_scale;
fh_height = designSize.height;
fh_scale = 1/pxRatio/fh_design_scale;
props.fw = {width:fw_width,height:fw_height,scale:fw_scale,navHeight};
props.fh = {width:fh_width,height:fh_height,scale:fh_scale,navHeight};
console.log("winSize",JSON.stringify({width,height}))
console.log("winPixelSize",JSON.stringify({width:w,height:h}))
console.log("pxRatio",pxRatio)
console.log("fixWidth",JSON.stringify(props.fw))
console.log("fixHeight",JSON.stringify(props.fh))
}
static FixWidthView = (p) => {
let {width,height,scale,navHeight} = props.fw;
return (
<View {...p} style={[p.style,{
marginTop:navHeight,
width:width,
height:height,
transform:[{translateX:-width*.5},
{translateY:-height*.5},
{scale:scale},
{translateX:width*.5},
{translateY:height*.5}]
}]}/>
);
};
static FixHeightView = (p) => {
let {width,height,scale,navHeight} = props.fh;
return (
<View {...p} style={[p.style,{
marginTop:navHeight,
width:width,
height:height,
transform:[{translateX:-width*.5},
{translateY:-height*.5},
{scale:scale},
{translateX:width*.5},
{translateY:height*.5}]
}]}/>
);
};
};
//init
Resolution.setDesignSize();