【发布时间】:2021-09-22 01:06:54
【问题描述】:
我最近尝试了MotionLayout,当它是MotionLayout 的直接子级时,我可以在按钮上正常工作,但是当我将按钮包含在另一个布局中时,相同的运动场景不起作用,但父级布局仍然是MotionLayout。
按钮是直接子元素的第一个布局:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
app:layoutDescription="@xml/demo"
android:layout_height="match_parent"
tools:context=".Demo" >
<Button
android:layout_width="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content"
android:id="@+id/yellow_button"
/>
</androidx.constraintlayout.motion.widget.MotionLayout>
按钮是间接子元素的第二个布局:-
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.motion.widget.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
app:layoutDescription="@xml/demo"
android:layout_height="match_parent"
tools:context=".Demo"
>
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/l1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/yellow_button"/>
</LinearLayout>
</androidx.constraintlayout.motion.widget.MotionLayout>
运动场景布局如下:-
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="@+id/start">
<Constraint android:id="@+id/yellow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<CustomAttribute app:attributeName="alpha"
app:customFloatValue="0.0"/>
</Constraint>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint android:id="@id/yellow_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alpha="1.0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<CustomAttribute app:attributeName="alpha"
app:customFloatValue="1.0"/>
</Constraint>
</ConstraintSet>
<Transition
app:constraintSetEnd="@id/end"
app:autoTransition="animateToEnd"
app:constraintSetStart="@+id/start"
app:duration="2000"/>
在这些情况下是否需要遵循任何准则?
或
这是否意味着只有MotionLayout 的直接子级可以使用它来制作动画?
【问题讨论】:
标签: android animation android-motionlayout android-motionscene