I started to use the "new" constraint option from ConstraintLayout
, the circle constraint.
I would like to apply the value for the layout_constraintCircleRadius
attribute programmatically, once I will be calculating the view's radius also programmatically.
I have tried many different ways using the
public void constrainCircle (int viewId,int id,int radius,float angle)
method described from the document.
I also searched on many forums about it but I could not find anything. Have someone ever experienced such problem?
<android.support.constraint.ConstraintLayout
android:id="@+id/circle_constraint"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintWidth_percent="0.8"
app:layout_constraintDimensionRatio="W,1:1"
android:background="@drawable/circle">
<View
android:id="@+id/circle_center"
android:layout_width="20dp"
android:layout_height="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#ff0000"/>
<View
android:id="@+id/circle_position_0"
android:layout_width="30dp"
android:layout_height="27dp"
android:background="#000000"
app:layout_constraintCircle="@id/circle_center"/>
</android.support.constraint.ConstraintLayout>
The circle_center will stay in the middle of the main constraint view and I would like to apply programatically the radius and angle to the circle_position_0.
Thank You!
If you want to change circleRadius
once than get ConstraintLayout.LayoutParams from the view and set your circleRadius
property value. Finally apply LayoutParams
to the view .
Sample Code:
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) fab1.getLayoutParams();
layoutParams.circleRadius = 300;
fab1.setLayoutParams(layoutParams);
If you want to animate circleRadius
, you can use ValueAnimator for animation. In onAnimationUpdate
method apply new circleRadius
to ConstraintLayout.LayoutParams
.
Sample Code:
private ValueAnimator getAnimator(final FloatingActionButton fab, long duration) {
ValueAnimator anim = ValueAnimator.ofInt(150, 300);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
int val = (Integer) valueAnimator.getAnimatedValue();
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) fab.getLayoutParams();
layoutParams.circleRadius = val;
fab.setLayoutParams(layoutParams);
}
});
anim.setDuration(duration);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.setRepeatCount(ValueAnimator.INFINITE);
return anim;
}
ValueAnimator valueAnimator1 = getAnimator(fab1, 1000);
valueAnimator1.start();
Consider using this code for applying ConstraintSet
programmatically:
ConstraintLayout cl = (ConstraintLayout) findViewById(R.id.circle_constraint); // No need if you have already done.
ConstraintSet c = new ConstraintSet(); // Make new ConstraintSet.
c.clone(cl); // Cloning from our ConstraintLayout or else our ConstraintSet won't get affect on ConstraintLayout.
c.constrainCircle(R.id.circle_position_0, R.id.circle_center, 40, 45); // Applying our circle constraint, use anything replacing 40 for radius & 45 for any angle.
c.applyTo(cl); // Apply back our ConstraintSet on ConstraintLayout.
You'll require to clone constraint set from existing layout for which you want to add constraint to & then apply back to your layout once done.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With