-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
71 lines (56 loc) · 1.31 KB
/
index.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
import {
Dimensions,
PixelRatio,
} from 'react-native';
const dpW = Dimensions.get('window').width;
const dpH = Dimensions.get('window').height;
const pixDens = PixelRatio.get();
export const units = {
PIXELS : 0,
WPERC : 1,
HPERC: 2,
INCH: 3,
MM: 4,
DP: 5
}
export class Size {
constructor(size, type) {
if (type == units.PIXELS) {
this.dp = size / pixDens;
} else if (type == units.WPERC) {
this.dp = dpW * size / 100;
} else if (type == units.HPERC) {
this.dp = dpH * size / 100;
} else if (type == units.INCH) {
this.dp = size * 160;
} else if (type == units.MM) {
this.dp = size * 160 / 25.4;
} else if (type == units.DP) {
this.dp = size;
} else {
console.log("unknown type: " + type)
}
}
static pixelsToDP(size) {
return size / pixDens;
}
static widthPercToDP(size) {
return dpW * size / 100;
}
static heightPercToDP(size) {
return dpH * size / 100;
}
static inchToDP(size) {
return size * 160;
}
static mmToDP(size) {
return size * 160 / 25.4;
}
getSize() {
return this.dp
}
}
module.exports = {
Size: Size,
units: units
}