-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmodule-CustomView.js
116 lines (113 loc) · 4.24 KB
/
module-CustomView.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
importClass(java.io.FileOutputStream);
importClass(java.io.File);
importClass(android.graphics.PaintFlagsDrawFilter);
importClass(android.graphics.Color);
importClass(android.animation.ObjectAnimator);
importClass(android.animation.AnimatorListenerAdapter);
importClass(android.util.TypedValue);
importClass(android.graphics.PorterDuffXfermode);
importClass(android.graphics.Bitmap);
importClass(android.graphics.Region);
importClass(android.graphics.PorterDuff);
importClass(android.graphics.Xfermode);
importClass(android.graphics.Rect);
importClass(android.graphics.Path);
importClass(android.graphics.drawable.ColorDrawable);
/* -------------------------------------------------------------------------- */
(function () {
util.extend(CustomView, ui.Widget);
function CustomView() {
ui.Widget.call(this);
let scope = this;
let paint = new Paint(Paint.ANTI_ALIAS_FLAG);
let rect = new android.graphics.RectF();
paint.setAntiAlias(true);
paint.setAlpha(255); //透明度0-255 0完全透明 255完全不透明
paint.setStyle(android.graphics.Paint.Style.FILL);
let deviation = 12;
let checkedColor = "#27ae60";
let unCheckedColor = "#bdc3c7";
this.checked = false;
this.defineAttr("checked", (view, attr, value, defineSetter) => {
if (value === "false") {
this.checked = false;
} else if (value === "true") {
this.checked = true;
}
this.updateColor();
});
this.defineAttr("checkedColor", (view, attr, value, defineSetter) => {
checkedColor = value;
});
this.defineAttr("unCheckedColor", (view, attr, value, defineSetter) => {
unCheckedColor = value;
});
this.setChecked = function (value) {
this.checked = value;
this.updateColor();
};
this.getChecked = function () {
return this.checked;
};
this.updateColor = function () {
if (this.checked) {
setBackground(scope.view, checkedColor);
} else {
setBackground(scope.view, unCheckedColor);
}
};
this.onFinishInflation = function (view) {
view.post(function () {
scope.updateColor();
});
};
function setBackground(view, color) {
let viewWidth = view.getMeasuredWidth();
let viewHeight = view.getMeasuredHeight();
let centerX = viewWidth / 2;
let centerY = viewHeight / 2;
let radius = viewWidth / 2;
var drawable = new android.graphics.drawable.Drawable({
draw: function (canvas) {
let 圆形内部正方形 = {
cx: viewWidth / 2,
cy: viewHeight / 2,
sideLength: (viewWidth / 5) * 3,
};
let 对勾的起点 = {
x: 圆形内部正方形.cx - 圆形内部正方形.sideLength / 2,
y: 圆形内部正方形.cy,
};
let 对勾的中点 = {
x: 圆形内部正方形.cx,
y: 圆形内部正方形.cy + 圆形内部正方形.sideLength / 2,
};
let 对勾的尾点 = {
x: 圆形内部正方形.cx + 圆形内部正方形.sideLength / 2,
y: 圆形内部正方形.cy - 圆形内部正方形.sideLength / 2,
};
let circle = new Path();
circle.reset();
let k = deviation;
circle.moveTo(对勾的起点.x, 对勾的起点.y);
circle.lineTo(对勾的中点.x, 对勾的中点.y);
circle.lineTo(对勾的尾点.x, 对勾的尾点.y);
circle.lineTo(对勾的尾点.x - k / 2, 对勾的尾点.y);
circle.lineTo(对勾的中点.x - k / 4, 对勾的中点.y - k * 1.5);
circle.lineTo(对勾的起点.x + k, 对勾的起点.y);
circle.close();
canvas.clipPath(circle, Region.Op.DIFFERENCE);
let backgroundColor = colors.parseColor(color);
paint.setColor(backgroundColor);
canvas.drawCircle(centerX, centerY, radius, paint);
},
});
view.setBackgroundDrawable(drawable);
}
}
CustomView.prototype.render = function () {
return <View w="30dp" h="30dp" margin="6 6 6 6"></View>;
};
ui.registerWidget("custom-checkbox", CustomView);
return CustomView;
})();