-
Notifications
You must be signed in to change notification settings - Fork 176
/
CircularFlow.java
498 lines (449 loc) · 15.1 KB
/
CircularFlow.java
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.constraintlayout.helper.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.constraintlayout.widget.R;
import androidx.constraintlayout.widget.VirtualLayout;
import java.util.Arrays;
/**
*
* CircularFlow virtual layout.
*
* Allows positioning of referenced widgets circular.
*
* The elements referenced are indicated via constraint_referenced_ids, as with other ContraintHelper implementations.
*
* XML attributes that are needed:
* <ul>
* <li>constraint_referenced_ids = "view2, view3, view4,view5,view6"</li>. It receives id's of the views that will add the references.
* <li>circularflow_viewCenter = "view1"</li>. It receives the id of the view of the center where the views received in constraint_referenced_ids will be referenced.
* <li>circularflow_angles = "45,90,135,180,225"</li>. Receive the angles that you will assign to each view.
* <li>circularflow_radiusInDP = "90,100,110,120,130"</li>. Receive the radios in DP that you will assign to each view.
* </ul>
*
* Example in XML:
* <androidx.constraintlayout.helper.widget.CircularFlow
* android:id="@+id/circularFlow"
* android:layout_width="match_parent"
* android:layout_height="match_parent"
* app:circularflow_angles="0,40,80,120"
* app:circularflow_radiusInDP="90,100,110,120"
* app:circularflow_viewCenter="@+id/view1"
* app:constraint_referenced_ids="view2,view3,view4,view5" />
*
* DEFAULT radius - If you add a view and don't set its radius, the default value will be 0.
* DEFAULT angles - If you add a view and don't set its angle, the default value will be 0.
*
* Recommendation - always set radius and angle for all views in <i>constraint_referenced_ids</i>
*
* */
public class CircularFlow extends VirtualLayout {
private static final String TAG = "CircularFlow";
ConstraintLayout mContainer;
int mViewCenter;
private static int DEFAULT_RADIUS = 0;
private static float DEFAULT_ANGLE = 0F;
/**
* @hide
*/
private float[] mAngles;
/**
* @hide
*/
private int[] mRadius;
/**
* @hide
*/
private int mCountRadius;
/**
* @hide
*/
private int mCountAngle;
/**
* @hide
*/
private String mReferenceAngles;
/**
* @hide
*/
private String mReferenceRadius;
/**
* @hide
*/
private Float mReferenceDefaultAngle;
/**
* @hide
*/
private Integer mReferenceDefaultRadius;
public CircularFlow(Context context) {
super(context);
}
public CircularFlow(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircularFlow(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public int[] getRadius() {
return Arrays.copyOf(mRadius, mCountRadius);
}
public float[] getAngles() {
return Arrays.copyOf(mAngles, mCountAngle);
}
@Override
protected void init(AttributeSet attrs) {
super.init(attrs);
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ConstraintLayout_Layout);
final int N = a.getIndexCount();
for (int i = 0; i < N; i++) {
int attr = a.getIndex(i);
if (attr == R.styleable.ConstraintLayout_Layout_circularflow_viewCenter) {
mViewCenter = a.getResourceId(attr, 0);
} else if (attr == R.styleable.ConstraintLayout_Layout_circularflow_angles) {
mReferenceAngles = a.getString(attr);
setAngles(mReferenceAngles);
} else if (attr == R.styleable.ConstraintLayout_Layout_circularflow_radiusInDP) {
mReferenceRadius = a.getString(attr);
setRadius(mReferenceRadius);
} else if (attr == R.styleable.ConstraintLayout_Layout_circularflow_defaultAngle) {
mReferenceDefaultAngle = a.getFloat(attr, DEFAULT_ANGLE);
setDefaultAngle(mReferenceDefaultAngle);
} else if (attr == R.styleable.ConstraintLayout_Layout_circularflow_defaultRadius) {
mReferenceDefaultRadius = a.getDimensionPixelSize(attr, DEFAULT_RADIUS);
setDefaultRadius(mReferenceDefaultRadius);
}
}
a.recycle();
}
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
if (mReferenceAngles != null) {
mAngles = new float[1];
setAngles(mReferenceAngles);
}
if (mReferenceRadius != null) {
mRadius = new int[1];
setRadius(mReferenceRadius);
}
if (mReferenceDefaultAngle != null) {
setDefaultAngle(mReferenceDefaultAngle);
}
if (mReferenceDefaultRadius != null) {
setDefaultRadius(mReferenceDefaultRadius);
}
anchorReferences();
}
private void anchorReferences() {
mContainer = (ConstraintLayout) getParent();
for (int i = 0; i < mCount; i++) {
View view = mContainer.getViewById(mIds[i]);
if (view == null) {
continue;
}
int radius = DEFAULT_RADIUS;
float angle = DEFAULT_ANGLE;
if (mRadius != null && i < mRadius.length) {
radius = mRadius[i];
} else if (mReferenceDefaultRadius != null && mReferenceDefaultRadius != -1) {
mCountRadius++;
if (mRadius == null) {
mRadius = new int[1];
}
mRadius = getRadius();
mRadius[mCountRadius - 1] = radius;
} else {
Log.e("CircularFlow", "Added radius to view with id: " + mMap.get(view.getId()));
}
if (mAngles != null && i < mAngles.length) {
angle = mAngles[i];
} else if (mReferenceDefaultAngle != null && mReferenceDefaultAngle != -1) {
mCountAngle++;
if (mAngles == null) {
mAngles = new float[1];
}
mAngles = getAngles();
mAngles[mCountAngle - 1] = angle;
} else {
Log.e("CircularFlow", "Added angle to view with id: " + mMap.get(view.getId()));
}
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
params.circleAngle = angle;
params.circleConstraint = mViewCenter;
params.circleRadius = radius;
view.setLayoutParams(params);
}
applyLayoutFeatures();
}
/**
* Add a view to the CircularFlow. The referenced view need to be a child of the container parent.
* The view also need to have its id set in order to be added.
* The views previous need to have its radius and angle set in order to be added correctly a new view.
* @param view
* @param radius
* @param angle
* @return
*/
public void addViewToCircularFlow(View view, int radius, float angle) {
if (containsId(view.getId())){
return;
}
addView(view);
mCountAngle++;
mAngles = getAngles();
mAngles[mCountAngle - 1] = angle;
mCountRadius++;
mRadius = getRadius();
mRadius[mCountRadius - 1] = (int) (radius * myContext.getResources().getDisplayMetrics().density);
anchorReferences();
}
/**
* Update radius from a view in CircularFlow. The referenced view need to be a child of the container parent.
* The view also need to have its id set in order to be added.
* @param view
* @param radius
* @return
*/
public void updateRadius(View view, int radius) {
if (!isUpdatable(view)) {
Log.e("CircularFlow", "It was not possible to update radius to view with id: " + view.getId());
return;
}
int indexView = indexFromId(view.getId());
if (indexView > mRadius.length) {
return;
}
mRadius = getRadius();
mRadius[indexView] = (int) (radius * myContext.getResources().getDisplayMetrics().density);
anchorReferences();
}
/**
* Update angle from a view in CircularFlow. The referenced view need to be a child of the container parent.
* The view also need to have its id set in order to be added.
* @param view
* @param angle
* @return
*/
public void updateAngle(View view, float angle) {
if (!isUpdatable(view)) {
Log.e("CircularFlow", "It was not possible to update angle to view with id: " + view.getId());
return;
}
int indexView = indexFromId(view.getId());
if (indexView > mAngles.length) {
return;
}
mAngles = getAngles();
mAngles[indexView] = angle;
anchorReferences();
}
/**
* Update angle and radius from a view in CircularFlow. The referenced view need to be a child of the container parent.
* The view also need to have its id set in order to be added.
* @param view
* @param radius
* @param angle
* @return
*/
public void updateReference(View view, int radius, float angle) {
if (!isUpdatable(view)) {
Log.e("CircularFlow", "It was not possible to update radius and angle to view with id: " + view.getId());
return;
}
int indexView = indexFromId(view.getId());
if (getAngles().length > indexView) {
mAngles = getAngles();
mAngles[indexView] = angle;
}
if (getRadius().length > indexView) {
mRadius = getRadius();
mRadius[indexView] = (int) (radius * myContext.getResources().getDisplayMetrics().density);
}
anchorReferences();
}
/**
* Set default Angle for CircularFlow.
*
* @param angle
* @return
*/
public void setDefaultAngle(float angle) {
DEFAULT_ANGLE = angle;
}
/**
* Set default Radius for CircularFlow.
*
* @param radius
* @return
*/
public void setDefaultRadius(int radius) {
DEFAULT_RADIUS = radius;
}
@Override
public int removeView(View view) {
int index = super.removeView(view);
if (index == -1) {
return index;
}
ConstraintSet c = new ConstraintSet();
c.clone(mContainer);
c.clear(view.getId(), ConstraintSet.CIRCLE_REFERENCE);
c.applyTo(mContainer);
if (index < mAngles.length) {
mAngles = removeAngle(mAngles, index);
mCountAngle--;
}
if (index < mRadius.length) {
mRadius = removeRadius(mRadius, index);
mCountRadius--;
}
anchorReferences();
return index;
}
/**
* @hide
*/
private float[] removeAngle(float[] angles, int index) {
if (angles == null
|| index < 0
|| index >= mCountAngle) {
return angles;
}
return removeElementFromArray(angles, index);
}
/**
* @hide
*/
private int[] removeRadius(int[] radius, int index) {
if (radius == null
|| index < 0
|| index >= mCountRadius) {
return radius;
}
return removeElementFromArray(radius, index);
}
/**
* @hide
*/
private void setAngles(String idList) {
if (idList == null) {
return;
}
int begin = 0;
mCountAngle = 0;
while (true) {
int end = idList.indexOf(',', begin);
if (end == -1) {
addAngle(idList.substring(begin).trim());
break;
}
addAngle(idList.substring(begin, end).trim());
begin = end + 1;
}
}
/**
* @hide
*/
private void setRadius(String idList) {
if (idList == null) {
return;
}
int begin = 0;
mCountRadius = 0;
while (true) {
int end = idList.indexOf(',', begin);
if (end == -1) {
addRadius(idList.substring(begin).trim());
break;
}
addRadius(idList.substring(begin, end).trim());
begin = end + 1;
}
}
/**
* @hide
*/
private void addAngle(String angleString) {
if (angleString == null || angleString.length() == 0) {
return;
}
if (myContext == null) {
return;
}
if (mAngles == null) {
return;
}
if (mCountAngle + 1 > mAngles.length) {
mAngles = Arrays.copyOf(mAngles, mAngles.length + 1);
}
mAngles[mCountAngle] = Integer.parseInt(angleString);
mCountAngle++;
}
/**
* @hide
*/
private void addRadius(String radiusString) {
if (radiusString == null || radiusString.length() == 0) {
return;
}
if (myContext == null) {
return;
}
if (mRadius == null) {
return;
}
if (mCountRadius + 1 > mRadius.length) {
mRadius = Arrays.copyOf(mRadius, mRadius.length + 1);
}
mRadius[mCountRadius] = (int) (Integer.parseInt(radiusString) * myContext.getResources().getDisplayMetrics().density);
mCountRadius++;
}
public static int[] removeElementFromArray(int[] array, int index) {
int[] newArray = new int[array.length - 1];
for (int i = 0, k = 0; i < array.length; i++) {
if (i == index) {
continue;
}
newArray[k++] = array[i];
}
return newArray;
}
public static float[] removeElementFromArray(float[] array, int index) {
float[] newArray = new float[array.length - 1];
for (int i = 0, k = 0; i < array.length; i++) {
if (i == index) {
continue;
}
newArray[k++] = array[i];
}
return newArray;
}
public boolean isUpdatable(View view) {
if (!containsId(view.getId())){
return false;
}
int indexView = indexFromId(view.getId());
return indexView != -1;
}
}