Android圆以编程方式约束半径

时间:2018-10-03 05:10:23

标签: android android-layout android-constraintlayout

我开始使用ConstraintLayout中的“新”约束选项,即圆约束

我想以编程方式应用layout_constraintCircleRadius属性的值,一旦我将以编程方式计算视图的半径。

我已经使用

尝试了多种方法

public void constrainCircle (int viewId,int id,int radius,float angle)

document中描述的方法。

我也在许多论坛上进行了搜索,但找不到任何东西。有人遇到过这样的问题吗?

<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>

circle_center将停留在主约束视图的中间,我想以编程方式将圆角和角度应用于circle_position_0。

谢谢!

2 个答案:

答案 0 :(得分:0)

如果要更改一次circleRadius而不是从视图中获取ConstraintLayout.LayoutParams并设置circleRadius属性值。最后将LayoutParams应用于视图。

示例代码:

    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) fab1.getLayoutParams();
    layoutParams.circleRadius = 300;
    fab1.setLayoutParams(layoutParams);

如果要制作circleRadius的动画,则可以使用ValueAnimator进行动画处理。在onAnimationUpdate方法中,将新的circleRadius应用于ConstraintLayout.LayoutParams

示例代码:

  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();

答案 1 :(得分:0)

考虑使用此代码以编程方式应用 ConstraintSet

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.

您需要从要为其添加约束的现有布局中克隆约束集,然后在完成后将其应用于您的布局。