-
Notifications
You must be signed in to change notification settings - Fork 97
/
ScreenUtil.js
351 lines (316 loc) · 10.5 KB
/
ScreenUtil.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/**
* 屏幕工具类 以及一些常用的工具类封装
* ui设计基准,iphone 6 2倍图
* width:750px
* height:1334px
* @2x
*/
import {
PixelRatio,
Dimensions,
Platform,
AsyncStorage
} from 'react-native';
export let screenW = Dimensions.get('window').width;
export let screenH = Dimensions.get('window').height;
const fontScale = PixelRatio.getFontScale();
export let pixelRatio = PixelRatio.get();
//像素密度
export const DEFAULT_DENSITY = 2;
//px转换成dp
//以iphone6为基准,如果以其他尺寸为基准的话,请修改下面的defaultWidth和defaultHeight为对应尺寸即可. 以下为1倍图时
const defaultWidth = 375;
const defaultHeight = 667;
const w2 = defaultWidth / DEFAULT_DENSITY;
//px转换成dp
const h2 = defaultHeight / DEFAULT_DENSITY;
//缩放比例
const _scaleWidth = screenW / defaultWidth;
const _scaleHeight = screenH / defaultHeight;
// iPhoneX
const X_WIDTH = 375;
const X_HEIGHT = 812;
/**
* 屏幕适配,缩放size , 默认根据宽度适配,纵向也可以使用此方法
* 横向的尺寸直接使用此方法
* 如:width ,paddingHorizontal ,paddingLeft ,paddingRight ,marginHorizontal ,marginLeft ,marginRight
* @param size 设计图的尺寸
* @returns {number}
*/
export function scaleSize(size: Number) {
return size * _scaleWidth;
}
/**
* 屏幕适配 , 纵向的尺寸使用此方法应该会更趋近于设计稿
* 如:height ,paddingVertical ,paddingTop ,paddingBottom ,marginVertical ,marginTop ,marginBottom
* @param size 设计图的尺寸
* @returns {number}
*/
export function scaleHeight(size: Number) {
return size * _scaleHeight;
}
/* 最初版本尺寸适配方案 也许你会更喜欢这个
export function scaleSize(size: Number) {
let scaleWidth = screenW / w2;
let scaleHeight = screenH / h2;
let scale = Math.min(scaleWidth, scaleHeight);
size = Math.round((size * scale + 0.5));
return size / DEFAULT_DENSITY;
}*/
/**
* 设置字体的size(单位px)
* @param size 传入设计稿上的px , allowFontScaling 是否根据设备文字缩放比例调整,默认不会
* @returns {Number} 返回实际sp
*/
function setSpText(size: Number, allowFontScaling = false) {
const scale = Math.min(_scaleWidth, _scaleHeight);
const fontSize = allowFontScaling ? 1 : fontScale;
return size * scale / fontSize;
}
export function setSpText2(size: Number) {
let scaleWidth = screenW / w2;
let scaleHeight = screenH / h2;
let scale = Math.min(scaleWidth, scaleHeight);
size = Math.round((size * scale + 0.5));
return size / DEFAULT_DENSITY * fontScale;
}
/**
* 判断是否为iphoneX
* @returns {boolean}
*/
export function isIphoneX() {
return (
Platform.OS === 'ios' &&
((screenH === X_HEIGHT && screenW === X_WIDTH) ||
(screenH === X_WIDTH && screenW === X_HEIGHT))
)
}
/**
* 根据是否是iPhoneX返回不同的样式
* @param iphoneXStyle
* @param iosStyle
* @param androidStyle
* @returns {*}
*/
export function ifIphoneX(iphoneXStyle, iosStyle = {}, androidStyle) {
if (isIphoneX()) {
return iphoneXStyle;
} else if (Platform.OS === 'ios') {
return iosStyle
} else {
if (androidStyle) return androidStyle;
return iosStyle
}
}
/**
* 判断对象,数组,字符串是否为空
* @param str (null|undefined|''|' '|[]|{}) 均判断为空,返回true
* @returns {boolean}
*/
export function isEmpty(str) {
if (!str) {
return true;
} else if (typeof str === 'object' && Object.keys(str).length === 0) {
return true;
} else if (str.replace(/(^\s*)|(\s*$)/g, "").length === 0) {
return true;
}
return false;
}
//时间处理
Date.prototype.format = function (format) {
let date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if (/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for (let k in date) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length === 1
? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
};
//获取时间差 current:1497235409744 当前时间 start:1497235419744 开始时间
export function getRemainingime(current: Number, start: Number) {
let time = start - current;
if (time < 0) {
return ["0", "0", "0", "0", "0", "0"];
}
let year = Math.floor(time / (365 * 30 * 24 * 3600 * 1000));//年
let month = Math.floor(time / (30 * 24 * 3600 * 1000));//月
let days = Math.floor(time / (24 * 3600 * 1000));//日
let temp1 = time % (24 * 3600 * 1000);
let temp2 = temp1 % (3600 * 1000);
let minutes = Math.floor(temp2 / (60 * 1000));//分
let hours = Math.floor(temp1 / (3600 * 1000));//时
let temp3 = temp2 % (60 * 1000);
let seconds = Math.round(temp3 / 1000);//秒
let strs = [year, toNormal(month), toNormal(days), toNormal(hours), toNormal(minutes), toNormal(seconds)];
return strs;//["0", "0", "2", "7", "33", "30"]0年0月2日 7时33分30秒
}
//1497235419
export function getRemainingimeDistance(distance: Number) {
let time = distance * 1000;
if (time < 0) {
return ["0", "0", "0", "0", "0", "0"];
}
let year = Math.floor(time / (365 * 30 * 24 * 3600 * 1000));//年
let month = Math.floor(time / (30 * 24 * 3600 * 1000));//月
let days = Math.floor(time / (24 * 3600 * 1000));//日
let temp1 = time % (24 * 3600 * 1000);
let hours = Math.floor(temp1 / (3600 * 1000));//时
let temp2 = temp1 % (3600 * 1000);
let minutes = Math.floor(temp2 / (60 * 1000));//分
let temp3 = temp2 % (60 * 1000);
let seconds = Math.round(temp3 / 1000);//秒
let strs = [year, toNormal(month), toNormal(days), toNormal(hours), toNormal(minutes), toNormal(seconds)];
// strs.splice(0, 1, String(Number(strs[0]) - 1970));//年
// strs.splice(1, 1, String(Number(strs[1]) - 1));
// strs.splice(2, 1, (Number(strs[2]) - 1) < 10 ? '0' + (Number(strs[2]) - 1) : String(Number(strs[2]) - 1));
// strs.splice(3, 1, (Number(strs[3]) - 8) < 10 ? '0' + (Number(strs[3]) - 8) : String(Number(strs[3]) - 8));
// strs.splice(4, 1, Number(strs[4]) < 10 ? '0' + Number(strs[4]) : String(Number(strs[4])));
// strs.splice(5, 1, Number(strs[5]) < 10 ? '0' + Number(strs[5]) : String(Number(strs[5])));
return strs;//["0", "0", "2", "7", "33", "30"]0年0月2日 7时33分30秒
}
export function toNormal(time: Number) {
return time >= 10 ? time : '0' + time;
}
//转换成日期
export function toDate(timestamp: Number, format1 = 'yyyy-MM-dd hh:mm:ss') {
try {
if (timestamp > 10000) {
let date = new Date();
date.setTime(timestamp);
return date.format(format1);//2014-07-10 10:21:12
} else {
return '';
}
} catch (erro) {
return '';
}
return '';
}
//转换成时间搓
export function toTimestamp(date: String) {
let timestamp = Date.parse(date);
return timestamp / 1000; // 1497233827569/1000
}
//CST时间=>转换成日期yyyy-MM-dd hh:mm:ss
export function getTaskTime(strDate) {
if (null == strDate || "" == strDate) {
return "";
}
let dateStr = strDate.trim().split(" ");
let strGMT = dateStr[0] + " " + dateStr[1] + " " + dateStr[2] + " " + dateStr[5] + " " + dateStr[3] + " GMT+0800";
let date = new Date(Date.parse(strGMT));
let y = date.getFullYear();
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
let minute = date.getMinutes();
minute = minute < 10 ? ('0' + minute) : minute;
let second = date.getSeconds();
second = second < 10 ? ('0' + second) : second;
return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
};
//1497235419
export function getRemainingimeDistance2(distance: Number) {
let time = distance;
let days = Math.floor(time / (24 * 3600 * 1000));
let temp1 = time % (24 * 3600 * 1000);
let hours = Math.floor(temp1 / (3600 * 1000));
let temp2 = temp1 % (3600 * 1000);
let minutes = Math.floor(temp2 / (60 * 1000));
if (time <= 60 * 1000) {
minutes = 1;
}
let temp3 = temp2 % (60 * 1000);
let seconds = Math.round(temp3 / 1000);
return [hours, minutes];//["0", "0", "2", "7", "33", "30"]0年0月2日 7时33分30秒
}
/**
* 存储
* @param key
* @param value
* @param successCallback
* @param errorCallback
*/
export function saveAsyncStorage(key, value, successCallback, errorCallback) {
AsyncStorage.setItem(key, value, error => {
if (error) {
errorCallback(error);
}
else {
successCallback();
}
})
}
/**
* 取值
* @param key
* @param successCallback
* @param errorCallback
*/
export function getAsyncStorage(key, successCallback, errorCallback) {
AsyncStorage.getItem(key, (error, result) => {
if (error) {
errorCallback(error);
}
else {
successCallback(result);
}
})
}
/**
* 删除对应key的
* @param key
* @param successCallback
* @param errorCallback
*/
export function removeAsyncStorage(key, successCallback, errorCallback) {
AsyncStorage.getItem(key, error => {
if (error) {
errorCallback(error);
}
else {
successCallback();
}
})
}
//
// // 将当前时间换成时间格式字符串
// var timestamp3 = 1403058804;
// var newDate = new Date();
// newDate.setTime(timestamp3);
// // Wed Jun 18 2014
// console.log(newDate.toDateString());
// // 2014-06-18T02:33:24.000Z
// console.log(newDate.toISOString());
// // 2014-06-18T02:33:24.000Z
// console.log(newDate.toJSON());
// // 2014年6月18日
// console.log(newDate.toLocaleDateString());
// // 2014年6月18日 上午10:33:24
// console.log(newDate.toLocaleString());
// // 上午10:33:24
// console.log(newDate.toLocaleTimeString());
// // Wed Jun 18 2014 10:33:24 GMT+0800 (中国标准时间)
// console.log(newDate.toString());
// // 10:33:24 GMT+0800 (中国标准时间)
// console.log(newDate.toTimeString());
// // Wed, 18 Jun 2014 02:33:24 GMT
// console.log(newDate.toUTCString());
// // 2014-07-10 10:21:12
// console.log(newDate.format('yyyy-MM-dd h:m:s'))