我的MotionLayout有两个小部件,一个在MotionLayout中描述,第二个在场景文件中。
布局文件:
<android.support.constraint.motion.MotionLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutDescription="@xml/scene_test">
<View
android:id="@+id/test_view_static"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#ff0000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/test_view_dynamic"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#00ff11" />
</android.support.constraint.motion.MotionLayout>
场景文件:
<MotionScene xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<Transition
app:constraintSetStart="@+id/start"
app:constraintSetEnd="@+id/end"/>
<ConstraintSet android:id="@id/start">
<Constraint
android:id="@+id/test_view_dynamic"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</ConstraintSet>
<ConstraintSet android:id="@id/end">
<Constraint
android:id="@id/test_view_dynamic"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</ConstraintSet>
</MotionScene>
然后在活动中我设定进度:
//onCreate, etc..
MotionLayout motionLayout = findViewById(R.id.motion_layout);
motionLayout.setProgress(0.5f);
动态视图位于正确的位置,但是静态视图失去了所有约束,但不受场景的影响,如here所述,仅场景文件中描述的窗口小部件应受到影响。
另一方面,假设您有一个带有十二个小部件的布局,但是您要制作动画的只有一个-Motion MotionScene中的ConstraintSet只需要包含该小部件的约束。
库版本:
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha2'
此处的示例项目:https://github.com/Anrimian/MotionLayoutIssueTest
更新:感谢Kélian's answer我找到了解决方法:
public static void setProgress(MotionLayout motionLayout, float progress) {
if (ViewCompat.isLaidOut(motionLayout)) {
motionLayout.setProgress(progress);
} else {
motionLayout.post(() -> motionLayout.setProgress(progress));
}
}
答案 0 :(得分:2)
这很奇怪,布局检查器显示test_view_static
的大小:宽度= 0,高度= 0。
我对您的代码进行了一些更新:在onCreate上的test_view_static
上添加了一个单击侦听器以执行进度,并且工作正常。
我尝试了另一件事:在我放入的onStart()方法中:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MotionLayout motionLayout = findViewById(R.id.motion_layout);
motionLayout.setProgress(0.5f);
}
}, 1000);
和MotionLayout的行为符合预期。就像您必须等待MotionLayout初始化才能更新进度一样。
实际上,您想要具有您在场景中描述的另一种开始布局。您可以执行以下操作,而无需使用:
更新进度<ConstraintSet android:id="@id/start">
<Constraint
android:id="@+id/test_view_dynamic"
android:layout_width="40dp"
android:layout_height="40dp"
motion:layout_constraintRight_toRightOf="parent"
motion:layout_constraintTop_toTopOf="parent"
motion:layout_constraintBottom_toBottomOf="parent" />
</ConstraintSet>