MotionLayout过渡时如何平滑改变视图的角半径?

问题描述 投票:0回答:2

我想在转换期间更改约束布局中的可绘制角半径,但我还没有找到如何使用 xml 来做到这一点,因为 CustomAttribute 标记仅支持可绘制颜色作为值。

<Constraint
            android:id="@+id/layout_player"
            android:layout_width="0dp"
            android:layout_height="42dp"
            android:background="@drawable/bg_player_control_part"
            motion:layout_constraintBottom_toBottomOf="@+id/layout_parent_audio_player_card"
            motion:layout_constraintEnd_toStartOf="@+id/tv_time_audio_small"
            motion:layout_constraintStart_toEndOf="@+id/img_photo_audio_news_small">

            <CustomAttribute
              motion:attributeName="background"
              motion:customColorDrawableValue="@drawable/bg_player_control_part" />

        </Constraint>
</ConstraintSet>

<ConstraintSet android:id="@+id/end">
<Constraint
            android:id="@+id/layout_player"
            android:layout_width="0dp"
            android:layout_height="42dp"
            motion:layout_constraintBottom_toBottomOf="@+id/layout_parent_audio_player_card"
            motion:layout_constraintEnd_toStartOf="@+id/tv_time_audio_small"
            motion:layout_constraintStart_toEndOf="@+id/img_photo_audio_news_small">

            <CustomAttribute
                motion:attributeName="background"
                motion:customColorDrawableValue="@color/color_bg_audio_player" />

        </Constraint>
</ConstraintSet>```
android animation android-motionlayout
2个回答
0
投票

一般来说,MotionLayout 只对 View 和 View 的方法进行操作。 可绘制对象不是视图。 但你可以通过两种方式做你需要的事情。

  1. 创建视图的子类并添加设置角的方法 半径。
  2. 我们在库中有一些视图,可让您对其角半径进行操作。 ImageFilterButton、ImageFilterView 和 MotionButton

然后可以使用以下方式调用它们:

<CustomAttribute motion:attributeName="round" motion:customDimension="4dp"/>
<CustomAttribute motion:attributeName="roundPercent" motion:customFloatValue="0.5"/>

分别调用 setRound(...) 和 setRoundPercent(..)


0
投票

如果您查看文档

CustomAttribute
支持以“set”开头的所有属性,那么棘手的部分以及您可能为此苦苦挣扎的原因是,设置
cornerRadius
的编程方法是
CardView.setRadius(float value)
,因此将其设置为您的 MotionScene xml 正在添加:

在你的开始约束中添加->

<CustomAttribute
                motion:attributeName="radius"
                motion:customFloatValue="0f" />

最后约束->

<CustomAttribute
            motion:attributeName="radius"
            motion:customFloatValue="38f" />
© www.soinside.com 2019 - 2024. All rights reserved.