Android 圆约束编程半径

标签 android android-layout android-constraintlayout

我开始使用 ConstraintLayout 中的"new"约束选项,圆形约束

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

我尝试过很多不同的方式来使用

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 将位于主约束 View 的中间,我想以编程方式将半径和角度应用到 circle_position_0。

谢谢!

最佳答案

如果你想改变一次circleRadius而不是得到ConstraintLayout.LayoutParams从 View 中设置您的 circleRadius 属性值。最后将 LayoutParams 应用于 View 。

示例代码:

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

关于Android 圆约束编程半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52620046/

相关文章:

android - YUV 解码功能错误或硬件问题?

android - 在不使用元数据、Android Google Drive Api 的情况下了解 Google 文档类型(工作表、幻灯片或文档)

android - 如何设置 ListView ?

java - 如何以编程方式创建约束布局

Android - 我可以使用 include/merge 标签提高 1-level-view-hierarchy-layout 的性能吗?

带有来自命令行的覆盖率报告的 Android 本地单元测试

android:contentInsetLeft ="0dp"与 app:contentInsetLeft ="0dp"

java - 如何将正确的上下文传递到新的 RelativeLayout 中?安卓java

android - 如何使用 android 布局中的工具替换设计器的合并项

android - addPreferencesFromResource 导致 FC : Error inflating class CheckboxPreference