【问题标题】:How to smoothly change corner radius of view during MotionLayout transition?如何在 MotionLayout 过渡期间平滑更改视角的角半径?
【发布时间】:2022-10-13 07:17:05
【问题描述】:

我想在转换期间更改约束布局中的可绘制角半径,但我还没有找到如何使用 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>```

【问题讨论】:

  • 您是否尝试过直接在约束本身上设置它?同样在@id/end 集中,您尝试将颜色设置为背景(颜色与半径有何关系?)。为开始/结束状态创建可绘制对象。
  • 我试图将 8dp 角半径更改为普通平面视图。所以在@id/end 里面我删除了drawable 并把颜色放到了视图中。但是当使用返回动画平面视图移动时,不会返回角视图。
  • 我明白了,我的建议是创建具有所需形状和颜色的平面可绘制对象并将其设置为该值。

标签: android animation android-motionlayout


【解决方案1】:

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

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

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

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

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

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 1970-01-01
    • 2014-06-12
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多