Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 72bad9e

Browse files
Improvement for CircularFlow (#139)
* Improvement for CircularFlow * example removeView Co-authored-by: Rodrigo Martin <[email protected]>
1 parent e2ca073 commit 72bad9e

File tree

3 files changed

+120
-4
lines changed

3 files changed

+120
-4
lines changed

constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/helper/widget/CircularFlow.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,73 @@ public void addViewToCircularFlow(View view, int radius, float angle) {
206206
anchorReferences();
207207
}
208208

209+
/**
210+
* Update radius from a view in CircularFlow. The referenced view need to be a child of the container parent.
211+
* The view also need to have its id set in order to be added.
212+
* @param view
213+
* @param radius
214+
* @return
215+
*/
216+
public void updateRadius(View view, int radius) {
217+
if (!isUpdatable(view)) {
218+
Log.e("CircularFlow", "It was not possible to update radius to view with id: " + view.getId());
219+
return;
220+
}
221+
int indexView = indexFromId(view.getId());
222+
if (indexView > mRadius.length) {
223+
return;
224+
}
225+
mRadius = getRadius();
226+
mRadius[indexView] = (int) (radius * myContext.getResources().getDisplayMetrics().density);
227+
anchorReferences();
228+
}
229+
230+
/**
231+
* Update angle from a view in CircularFlow. The referenced view need to be a child of the container parent.
232+
* The view also need to have its id set in order to be added.
233+
* @param view
234+
* @param angle
235+
* @return
236+
*/
237+
public void updateAngle(View view, float angle) {
238+
if (!isUpdatable(view)) {
239+
Log.e("CircularFlow", "It was not possible to update angle to view with id: " + view.getId());
240+
return;
241+
}
242+
int indexView = indexFromId(view.getId());
243+
if (indexView > mAngles.length) {
244+
return;
245+
}
246+
mAngles = getAngles();
247+
mAngles[indexView] = angle;
248+
anchorReferences();
249+
}
250+
251+
/**
252+
* Update angle and radius from a view in CircularFlow. The referenced view need to be a child of the container parent.
253+
* The view also need to have its id set in order to be added.
254+
* @param view
255+
* @param radius
256+
* @param angle
257+
* @return
258+
*/
259+
public void updateReference(View view, int radius, float angle) {
260+
if (!isUpdatable(view)) {
261+
Log.e("CircularFlow", "It was not possible to update radius and angle to view with id: " + view.getId());
262+
return;
263+
}
264+
int indexView = indexFromId(view.getId());
265+
if (getAngles().length > indexView) {
266+
mAngles = getAngles();
267+
mAngles[indexView] = angle;
268+
}
269+
if (getRadius().length > indexView) {
270+
mRadius = getRadius();
271+
mRadius[indexView] = (int) (radius * myContext.getResources().getDisplayMetrics().density);
272+
}
273+
anchorReferences();
274+
}
275+
209276
@Override
210277
public int removeView(View view) {
211278
int index = super.removeView(view);
@@ -361,4 +428,12 @@ public static float[] removeElementFromArray(float[] array, int index) {
361428
}
362429
return newArray;
363430
}
431+
432+
public boolean isUpdatable(View view) {
433+
if (!containsId(view.getId())){
434+
return false;
435+
}
436+
int indexView = indexFromId(view.getId());
437+
return indexView != -1;
438+
}
364439
}

constraintlayout/constraintlayout/src/main/java/androidx/constraintlayout/widget/ConstraintHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,4 +615,15 @@ public boolean containsId(final int id) {
615615
}
616616
return result;
617617
}
618+
619+
public int indexFromId(final int id) {
620+
int index = -1;
621+
for(int i : mIds) {
622+
index++;
623+
if(i == id){
624+
return index;
625+
}
626+
}
627+
return index;
628+
}
618629
}

projects/CarouselExperiments/app/src/main/java/androidx/constraintlayout/experiments/CircularFlowDemoActivity.kt

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package androidx.constraintlayout.experiments
33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
55
import android.view.View
6+
import android.widget.Toast
67
import androidx.constraintlayout.helper.widget.CircularFlow
78

89
class CircularFlowDemoActivity : AppCompatActivity() {
@@ -14,33 +15,62 @@ class CircularFlowDemoActivity : AppCompatActivity() {
1415
findViewById<CircularFlow>(R.id.circularFlow).addViewToCircularFlow(
1516
it, 130, 160F
1617
)
18+
Toast.makeText(
19+
it.context,
20+
"addViewToCircularFlow - Radius: " + 130 + " Angle: " + 160,
21+
Toast.LENGTH_SHORT
22+
).show()
1723
}
1824

1925
findViewById<View>(R.id.view7).setOnClickListener {
2026
findViewById<CircularFlow>(R.id.circularFlow).addViewToCircularFlow(
2127
it, 140, 200F
2228
)
29+
Toast.makeText(
30+
it.context,
31+
"AddViewToCircularFlow - Radius: " + 140 + " Angle: " + 200,
32+
Toast.LENGTH_SHORT
33+
).show()
2334
}
2435

2536
findViewById<View>(R.id.view8).setOnClickListener {
2637
findViewById<CircularFlow>(R.id.circularFlow).addViewToCircularFlow(
2738
it, 150, 240F
2839
)
40+
Toast.makeText(
41+
it.context,
42+
"addViewToCircularFlow - Radius: " + 150 + " Angle: " + 240,
43+
Toast.LENGTH_SHORT
44+
).show()
2945
}
3046

3147
findViewById<View>(R.id.view2).setOnClickListener {
32-
findViewById<CircularFlow>(R.id.circularFlow).removeView(
33-
it
48+
findViewById<CircularFlow>(R.id.circularFlow).updateAngle(
49+
it, 90F
3450
)
51+
Toast.makeText(it.context, "UpdateAngle Angle: " + 90, Toast.LENGTH_SHORT).show()
3552
}
3653

3754
findViewById<View>(R.id.view3).setOnClickListener {
38-
findViewById<CircularFlow>(R.id.circularFlow).removeView(
39-
it
55+
findViewById<CircularFlow>(R.id.circularFlow).updateRadius(
56+
it, 150
4057
)
58+
Toast.makeText(it.context, "UpdateRadius Radius: " + 150, Toast.LENGTH_SHORT).show()
59+
4160
}
4261

4362
findViewById<View>(R.id.view4).setOnClickListener {
63+
findViewById<CircularFlow>(R.id.circularFlow).updateReference(
64+
it, 160, 135F
65+
)
66+
Toast.makeText(
67+
it.context,
68+
"UpdateReference - Radius: " + 160 + " Angle: " + 135,
69+
Toast.LENGTH_SHORT
70+
).show()
71+
}
72+
73+
findViewById<View>(R.id.view5).setOnClickListener {
4474
findViewById<CircularFlow>(R.id.circularFlow).removeView(
4575
it
4676
)

0 commit comments

Comments
 (0)