MotionLayout能否实现分组可见性的动画效果?

7

我正在使用MotionLayout创建动画,需要隐藏一些元素。 尝试在单个元素中使用visibility属性可以实现,但为了使XML文件更短,我希望只指定包含所有这些元素的组(来自ConstraintLayout帮助程序)

类似这样:

<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <Transition
        app:constraintSetStart="@id/start"
        app:constraintSetEnd="@id/end"

        app:duration="300">

        <OnSwipe
            app:touchAnchorId="@id/details_group"
            app:touchAnchorSide="bottom"
            app:dragDirection="dragDown"
            />

    </Transition>

    <ConstraintSet
        android:id="@+id/start">

        <Constraint
            android:id="@+id/details_group"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="gone"
            app:constraint_referenced_ids="detail_value_topl,detail_icon_topl,detail_value_topr" />

    </ConstraintSet>

    <ConstraintSet
        android:id="@+id/end">

        <Constraint
            android:id="@+id/details_group"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:visibility="visible"
            app:constraint_referenced_ids="detail_value_topl,detail_icon_topl,detail_value_topr" />

    </ConstraintSet>

</MotionScene>

但它不起作用,有什么办法可以让它起作用吗?

另外,我更喜欢不使用alpha,因为所有约束都设置好了,当它们消失时容器会重新调整大小。


视图组是直接位于 MotionLayout 中的子项吗?请查看此处的限制条件:https://medium.com/google-developers/introduction-to-motionlayout-part-i-29208674b10d,如果适用于您的情况。 - Raghunandan
实际上,我也尝试过在约束组上进行动画处理,以减少我的代码量,但是它并没有起作用,所以我不得不单独处理它。 - Maulik Togadiya
1个回答

1
与其在约束上声明可见性,您应将可见性声明为自定义属性。因此,对于您的第一个约束,请尝试以下内容:
     <Constraint
        android:id="@+id/details_group"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:constraint_referenced_ids="detail_value_topl,detail_icon_topl,detail_value_topr">
        <CustomAttribute
            motion:attributeName="visibility"
            motion:customIntegerValue="8" />
    </Constraint>

"您的第二个限制尝试这个,保留HTML。"
    <Constraint
        android:id="@+id/details_group"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:constraint_referenced_ids="detail_value_topl,detail_icon_topl,detail_value_topr">
        <CustomAttribute
            motion:attributeName="visibility"
            motion:customIntegerValue="0" />
    </Constraint>

通过将可见性声明为自定义属性,应该能够帮助运动布局正确地在可见性值之间插值。虽然哪个int值代表哪个可见性有点不直观,但它们定义如下:
Visible = 0
Invisible = 4
Gone = 8

1
这种解决方案会导致视图在滑动转换启动时直接可见,而不是通过动画其alpha慢慢地混合。我会继续直接在约束上定义可见性属性。 - Palm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接