-
Notifications
You must be signed in to change notification settings - Fork 1
/
autodimens.gradle
87 lines (75 loc) · 3.13 KB
/
autodimens.gradle
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
/**
* Created by kienht - 1.7.2017
*/
//./gradlew createDimension
class AutoFactory extends DefaultTask {
@TaskAction
def create() {}
}
class DimensionFactory extends DefaultTask {
@Input
int[] dimens = [0, 320, 360, 384, 390, 400, 411, 420, 433, 450, 480, 533, 540, 561, 600, 640, 673, 681, 720, 768, 800, 840, 886, 960, 1024, 1080, 1280, 1366, 1440, 1600, 1920, 2560, 3840];
@Input
int fromDimension = 360;
@Input
double positiveMaxDP = 600
@Input
double negativeMaxDP = 600
@Input
double positiveMaxSP = 60
String resFolder = project.getProjectDir().getPath() + "/src/main/res/"
@TaskAction
def create() {
autoCreateDimen();
}
def autoCreateDimen() {
for (int dimen : dimens) {
String folderName = "";
String dimenFileName = "";
if (dimen == 0) {
dimen = fromDimension
folderName = "values"
dimenFileName = "\t<string name=\"dimenFileName\">values-default</string>\r"
} else {
folderName = "values-sw" + (int) dimen + "dp";
dimenFileName = "\t<string name=\"dimenFileName\">" + folderName + "</string>\r"
}
String folder = resFolder + folderName
String fileName = folder + "/auto_dimens.xml";
new File(folder).mkdir();
new File(fileName).createNewFile();
PrintWriter printWriter = new PrintWriter(fileName);
printWriter.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
printWriter.println("<resources>");
printWriter.println(dimenFileName);
double ratio = dimen / fromDimension;
double haftDp = ratio * 0.5;
printWriter.printf("\t<dimen name=\"_0_5dp\">%.2fdp</dimen>\r\n", haftDp);
for (int i = 1; i <= positiveMaxDP; i++) {
double dp = ratio * i;
printWriter.printf("\t<dimen name=\"_%ddp\">%.2fdp</dimen>\r\n", i, dp);
printWriter.printf("\t<dimen name=\"_%d_5dp\">%.2fdp</dimen>\r\n", i, dp + haftDp);
}
printWriter.printf("\t<dimen name=\"minus_0_5dp\">%.2fdp</dimen>\r\n", -haftDp);
for (int i = 1; i <= negativeMaxDP; i++) {
double dp = ratio * i;
printWriter.printf("\t<dimen name=\"minus_%ddp\">%.2fdp</dimen>\r\n", i, -dp);
printWriter.printf("\t<dimen name=\"minus_%d_5dp\">%.2fdp</dimen>\r\n", i, -dp - haftDp);
}
for (int i = 1; i <= positiveMaxSP; i++) {
double sp = ratio * i;
printWriter.printf("\t<dimen name=\"_%dsp\">%.2fsp</dimen>\r\n", i, sp);
}
printWriter.println("</resources>");
printWriter.close();
}
}
}
task createDimension(type: DimensionFactory) {}
createDimension {
dimens = [0, 320, 360, 384, 390, 400, 411, 420, 433, 450, 480, 533, 540, 561, 600, 640, 673, 681, 720, 768, 800, 840, 886, 960, 1024, 1080, 1280, 1366, 1440, 1600, 1920, 2560, 3840];
fromDimension = 360
positiveMaxDP = 600
negativeMaxDP = 600
positiveMaxSP = 60
}