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

Commit b46bd70

Browse files
Improvement to CircularFlow (#200)
* Improvement to CircularFlow * resolve verification_922.xml Co-authored-by: Rodrigo Martin <[email protected]>
1 parent 7d8eae1 commit b46bd70

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

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

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ public class CircularFlow extends VirtualLayout {
6969
/**
7070
* @hide
7171
*/
72-
private float[] mAngles = new float[32];
72+
private float[] mAngles;
7373

7474
/**
7575
* @hide
7676
*/
77-
private int[] mRadius = new int[32];
77+
private int[] mRadius;
7878

7979
/**
8080
* @hide
@@ -150,7 +150,7 @@ protected void init(AttributeSet attrs) {
150150
mReferenceDefaultAngle = a.getFloat(attr, DEFAULT_ANGLE);
151151
setDefaultAngle(mReferenceDefaultAngle);
152152
} else if (attr == R.styleable.ConstraintLayout_Layout_circularflow_defaultRadius) {
153-
mReferenceDefaultRadius = a.getInt(attr, DEFAULT_RADIUS);
153+
mReferenceDefaultRadius = a.getDimensionPixelSize(attr, DEFAULT_RADIUS);
154154
setDefaultRadius(mReferenceDefaultRadius);
155155
}
156156
}
@@ -162,9 +162,11 @@ protected void init(AttributeSet attrs) {
162162
public void onAttachedToWindow() {
163163
super.onAttachedToWindow();
164164
if (mReferenceAngles != null) {
165+
mAngles = new float[1];
165166
setAngles(mReferenceAngles);
166167
}
167168
if (mReferenceRadius != null) {
169+
mRadius = new int[1];
168170
setRadius(mReferenceRadius);
169171
}
170172
if (mReferenceDefaultAngle != null) {
@@ -178,41 +180,45 @@ public void onAttachedToWindow() {
178180

179181
private void anchorReferences() {
180182
mContainer = (ConstraintLayout) getParent();
181-
ConstraintSet c = new ConstraintSet();
182-
c.clone(mContainer);
183-
for (int i = 0; i <= mCount; i++) {
184-
int id = mIds[i];
185-
View view = mContainer.getViewById(id);
186-
187-
if (view != null) {
188-
int radius = DEFAULT_RADIUS;
189-
float angle = DEFAULT_ANGLE;
190-
191-
if (i < getRadius().length) {
192-
radius = getRadius()[i];
193-
} else if (mReferenceDefaultRadius != -1) {
194-
mCountRadius++;
195-
mRadius = getRadius();
196-
mRadius[mCountRadius - 1] = (int) (radius * myContext.getResources().getDisplayMetrics().density);
197-
radius = getRadius()[i];
198-
} else {
199-
Log.e("CircularFlow", "Added radius to view with id: " + mMap.get(view.getId()));
183+
for (int i = 0; i < mCount; i++) {
184+
View view = mContainer.getViewById(mIds[i]);
185+
if (view == null) {
186+
continue;
187+
}
188+
int radius = DEFAULT_RADIUS;
189+
float angle = DEFAULT_ANGLE;
190+
191+
if (mRadius != null && i < mRadius.length) {
192+
radius = mRadius[i];
193+
} else if (mReferenceDefaultRadius != null && mReferenceDefaultRadius != -1) {
194+
mCountRadius++;
195+
if (mRadius == null) {
196+
mRadius = new int[1];
200197
}
198+
mRadius = getRadius();
199+
mRadius[mCountRadius - 1] = radius;
200+
} else {
201+
Log.e("CircularFlow", "Added radius to view with id: " + mMap.get(view.getId()));
202+
}
201203

202-
if (i < getAngles().length) {
203-
angle = getAngles()[i];
204-
} else if (mReferenceDefaultAngle != -1) {
205-
mCountAngle++;
206-
mAngles = getAngles();
207-
mAngles[mCountAngle - 1] = angle;
208-
angle = getAngles()[i];
209-
} else {
210-
Log.e("CircularFlow", "Added angle to view with id: " + mMap.get(view.getId()));
204+
if (mAngles != null && i < mAngles.length) {
205+
angle = mAngles[i];
206+
} else if (mReferenceDefaultAngle != null && mReferenceDefaultAngle != -1) {
207+
mCountAngle++;
208+
if (mAngles == null) {
209+
mAngles = new float[1];
211210
}
212-
c.constrainCircle(view.getId(), mViewCenter, radius, angle);
211+
mAngles = getAngles();
212+
mAngles[mCountAngle - 1] = angle;
213+
} else {
214+
Log.e("CircularFlow", "Added angle to view with id: " + mMap.get(view.getId()));
213215
}
216+
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) view.getLayoutParams();
217+
params.circleAngle = angle;
218+
params.circleConstraint = mViewCenter;
219+
params.circleRadius = radius;
220+
view.setLayoutParams(params);
214221
}
215-
c.applyTo(mContainer);
216222
applyLayoutFeatures();
217223
}
218224

@@ -430,7 +436,7 @@ private void addAngle(String angleString) {
430436
}
431437

432438
if (mCountAngle + 1 > mAngles.length) {
433-
mAngles = Arrays.copyOf(mAngles, mAngles.length * 2);
439+
mAngles = Arrays.copyOf(mAngles, mAngles.length + 1);
434440
}
435441
mAngles[mCountAngle] = Integer.parseInt(angleString);
436442
mCountAngle++;
@@ -451,7 +457,7 @@ private void addRadius(String radiusString) {
451457
}
452458

453459
if (mCountRadius + 1 > mRadius.length) {
454-
mRadius = Arrays.copyOf(mRadius, mRadius.length * 2);
460+
mRadius = Arrays.copyOf(mRadius, mRadius.length + 1);
455461
}
456462

457463
mRadius[mCountRadius] = (int) (Integer.parseInt(radiusString) * myContext.getResources().getDisplayMetrics().density);

constraintlayout/constraintlayout/src/main/res/values/attrs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@
265265
<attr name="circularflow_angles" format="string"/>
266266
<attr name="circularflow_viewCenter" format="reference"/>
267267
<attr name="circularflow_defaultAngle" format="float"/>
268-
<attr name="circularflow_defaultRadius" format="integer"/>
268+
<attr name="circularflow_defaultRadius" format="dimension"/>
269269

270270

271271
<!-- TODO allows to use a drawable as horizontal separator between elements -->

projects/CarouselExperiments/app/src/main/res/layout/activity_circular_flow_demo.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@
7777
android:id="@+id/circularFlow"
7878
android:layout_width="match_parent"
7979
android:layout_height="match_parent"
80-
app:circularflow_angles="0,40,80,120"
81-
app:circularflow_radiusInDP="90,100,110,120"
80+
app:circularflow_defaultRadius="110dp"
81+
app:circularflow_defaultAngle="45"
82+
app:circularflow_radiusInDP="110, 150, 170, 190"
83+
app:circularflow_angles="10, 40, 80, 135"
8284
app:circularflow_viewCenter="@+id/view1"
8385
app:constraint_referenced_ids="view2,view3,view4,view5" />
8486
</androidx.constraintlayout.widget.ConstraintLayout>

projects/MotionLayoutVerification/app/src/main/res/layout/verification_922.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@
254254
android:id="@+id/circularFlow"
255255
android:layout_width="match_parent"
256256
android:layout_height="match_parent"
257-
app:circularflow_defaultRadius="140"
257+
app:circularflow_defaultRadius="140dp"
258258
app:circularflow_angles="0,0,15,30,45,60,75,90,105,120,135,150,165,180,195,210,225,240,255,270,285,300,315,330,345"
259259
app:circularflow_radiusInDP="116"
260260
app:circularflow_viewCenter="@id/earth"

0 commit comments

Comments
 (0)