Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android circle constraint programmatically radius

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!

like image 875
Lajos Neto Avatar asked Oct 03 '18 05:10

Lajos Neto


2 Answers

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();
like image 171
Abu Yousuf Avatar answered Nov 15 '22 11:11

Abu Yousuf


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.

like image 42
Jeel Vankhede Avatar answered Nov 15 '22 12:11

Jeel Vankhede