-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircleImageView.java
More file actions
362 lines (314 loc) · 11.4 KB
/
Copy pathCircleImageView.java
File metadata and controls
362 lines (314 loc) · 11.4 KB
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
352
353
354
355
356
357
358
359
360
361
362
package com.example.my_first_match_app.my_loop;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
public class CircleImageView extends AppCompatImageView {
/* 默认圆角大小为30 */
private int angle = 30;
/* 是否设置四个角为圆角 */
private boolean all;
/* 左上/左下/右上/右下角的圆角大小 */
private int leftTopAngle, leftBottomAngle, rightTopAngle, rightBottomAngle;
/* 是否左上/左下/右上/右下角为圆角 */
private boolean leftTop, leftBottom, rightTop, rightBottom;
/* 控件的宽和高 */
private float width, height;
/* 用于画矩形或者对矩形进行裁剪 */
private final Path path = new Path();
/* 矩形的样子, 多大, 坐标存放在此对象中 */
private final RectF rf = new RectF();
/* 创建一个8个字节长度的数组, 其中每两个数据代表正方形的一个点,[0],[1]代表左上角,以此类推 */
private final float[] rids = new float[8];
/* 画圆角的方向 CW为顺时针(左上/右上/右下/左下) CCW为逆时针(左下开始) */
private final Path.Direction direction = Path.Direction.CW;
public CircleImageView(@NonNull Context context) {
super(context);
}
public CircleImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CircleImageView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
width = getWidth();
height = getHeight();
}
/**
* 设置圆角大小, 此方法就默认代表四个角都设置为圆角
*
* @param angle 圆角大小
* @return CircleImageView
*/
public CircleImageView setAngle(int angle) {
this.angle = angle;
this.all = true;
return this;
}
/**
* 设置左上角的圆角
*
* @param leftTopAngle 左上角的圆角大小
* @return CircleImageView
*/
public CircleImageView setLeftTopAngle(int leftTopAngle) {
this.leftTopAngle = leftTopAngle;
leftTop = true;
return this;
}
/**
* 设置右上角的圆角
*
* @param rightTopAngle 右上角的圆角大小
* @return CircleImageView
*/
public CircleImageView setRightTopAngle(int rightTopAngle) {
this.rightTopAngle = rightTopAngle;
rightTop = true;
return this;
}
/**
* 设置右下角的圆角
*
* @param rightBottomAngle 右下角的圆角大小
* @return CircleImageView
*/
public CircleImageView setRightBottomAngle(int rightBottomAngle) {
this.rightBottomAngle = rightBottomAngle;
rightBottom = true;
return this;
}
/**
* 设置左下角的圆角
*
* @param leftBottomAngle 左下角的圆角大小
* @return CircleImageView
*/
public CircleImageView setLeftBottomAngle(int leftBottomAngle) {
this.leftBottomAngle = leftBottomAngle;
leftBottom = true;
return this;
}
/**
* 同时设置左上角和右上角的圆角
*
* @param leftTopAngle 左上角的圆角大小
* @param rightTopAngle 右上角的圆角大小
* @return CircleImageView
*/
public CircleImageView setLeftTopRightTop(int leftTopAngle, int rightTopAngle) {
this.leftTopAngle = leftTopAngle;
this.rightTopAngle = rightTopAngle;
leftTop = true;
rightTop = true;
return this;
}
/**
* 同时设置左上角和右下角的圆角
*
* @param leftTopAngle 左上角的圆角大小
* @param rightBottomAngle 右下角的圆角大小
* @return CircleImageView
*/
public CircleImageView setLeftTopRightBottom(int leftTopAngle, int rightBottomAngle) {
this.leftTopAngle = leftTopAngle;
this.rightBottomAngle = rightBottomAngle;
leftTop = true;
rightBottom = true;
return this;
}
/**
* 同时设置左上角和左下角的圆角
*
* @param leftTopAngle 左上角的圆角大小
* @param leftBottomAngle 右上角的圆角大小
* @return CircleImageView
*/
public CircleImageView setLeftTopLeftBottom(int leftTopAngle, int leftBottomAngle) {
this.leftTopAngle = leftTopAngle;
this.leftBottomAngle = leftBottomAngle;
leftTop = true;
leftBottom = true;
return this;
}
/**
* 同时设置左下角和右上角的圆角
*
* @param leftBottomAngle 左下角的圆角
* @param rightTopAngle 右上角的圆角
* @return CircleImageView
*/
public CircleImageView setLeftBottomRightTop(int leftBottomAngle, int rightTopAngle) {
this.leftBottomAngle = leftBottomAngle;
this.rightTopAngle = rightTopAngle;
leftBottom = true;
rightTop = true;
return this;
}
/**
* 同时设置左下角和右下角的圆角
*
* @param leftBottomAngle 左下角的圆角
* @param rightBottomAngle 右下角的圆角
* @return CircleImageView
*/
public CircleImageView setLeftBottomRightBottom(int leftBottomAngle, int rightBottomAngle) {
this.leftBottomAngle = leftBottomAngle;
this.rightBottomAngle = rightBottomAngle;
leftBottom = true;
rightBottom = true;
return this;
}
/**
* 同时设置右下角和右上角的圆角
*
* @param rightBottomAngle 右下角的圆角大小
* @param rightTopAngle 右上角的圆角大小
* @return CircleImageView
*/
public CircleImageView setRightBottomRightTop(int rightBottomAngle, int rightTopAngle) {
this.rightBottomAngle = rightBottomAngle;
this.rightTopAngle = rightTopAngle;
rightBottom = true;
rightTop = true;
return this;
}
private void applyLeftTop(Canvas canvas) {
rids[0] = this.leftTopAngle;
rids[1] = this.leftTopAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyRightTop(Canvas canvas) {
rids[2] = this.rightTopAngle;
rids[3] = this.rightTopAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyRightBottom(Canvas canvas) {
rids[4] = this.rightBottomAngle;
rids[5] = this.rightBottomAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyLeftBottom(Canvas canvas) {
rids[6] = this.leftBottomAngle;
rids[7] = this.leftBottomAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyAll(Canvas canvas) {
for (float i = 0; i < rids.length; i++) {
rids[(int) i] = angle;
}
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyLeftTopRightTop(Canvas canvas) {
rids[0] = leftTopAngle;
rids[1] = leftTopAngle;
rids[2] = rightTopAngle;
rids[3] = rightTopAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyLeftTopRightBottom(Canvas canvas) {
rids[0] = leftTopAngle;
rids[1] = leftTopAngle;
rids[4] = rightBottomAngle;
rids[5] = rightBottomAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyLeftTopLeftBottom(Canvas canvas) {
rids[0] = leftTopAngle;
rids[1] = leftTopAngle;
rids[6] = leftBottomAngle;
rids[7] = leftBottomAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyRightTopRightBottom(Canvas canvas) {
rids[2] = rightTopAngle;
rids[3] = rightTopAngle;
rids[4] = rightBottomAngle;
rids[5] = rightBottomAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyRightTopLeftBottom(Canvas canvas) {
rids[2] = rightTopAngle;
rids[3] = rightTopAngle;
rids[6] = leftBottomAngle;
rids[7] = leftBottomAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
private void applyRightBottomLeftBottom(Canvas canvas) {
rids[4] = rightBottomAngle;
rids[5] = rightBottomAngle;
rids[6] = leftBottomAngle;
rids[7] = leftBottomAngle;
rf.set(0, 0, width, height);
path.addRoundRect(rf, rids, direction);
canvas.clipPath(path);
}
@Override
protected void onDraw(Canvas canvas) {
if (leftTop && rightTop && leftBottom && rightBottom) {
applyAll(canvas);
}
if (leftTop && rightTop) {
applyLeftTopRightTop(canvas);
} else if (leftTop && rightBottom) {
applyLeftTopRightBottom(canvas);
} else if (leftTop && leftBottom) {
applyLeftTopLeftBottom(canvas);
} else if (rightTop && rightBottom) {
applyRightTopRightBottom(canvas);
} else if (rightTop && leftBottom) {
applyRightTopLeftBottom(canvas);
} else if (rightBottom && leftBottom) {
applyRightBottomLeftBottom(canvas);
}
if (all) {
applyAll(canvas);
} else if (leftTop) {
applyLeftTop(canvas);
} else if (rightTop) {
applyRightTop(canvas);
} else if (rightBottom) {
applyRightBottom(canvas);
} else if (leftBottom) {
applyLeftBottom(canvas);
}
// path.moveTo(angle, 0); // (30, 0)
// path.lineTo(width - angle, 0); // (1050, 0)
// path.quadTo(width, 0, width, angle); // (1050, 0, 1050, 30)
// path.lineTo(width, height - angle); // (1050, 630)
// path.quadTo(width, height, width - angle, height); // (1050, 630, 1020, 630)
// path.lineTo(angle, height); // (30, 630)
// path.quadTo(0, height, 0, height - angle); // (0, 1050, 0 1020)
// path.lineTo(0, angle); // (0, 30)
// path.quadTo(0, 0, 40, 0); // (0, 0, 40, 0)
// canvas.clipPath(path); // 截取这四组坐标, 这四组坐标是一个闭环, 相当手机中给照片截取圆角图片
super.onDraw(canvas);
}
}