android - 当手动将进度设置为 MotionLayout 时,它会清除所有约束
问题描述
我有带有两个小部件的 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);
动态视图在正确的位置,但静态视图失去了所有约束,但不受场景影响,如此处所述,只有场景文件中描述的小部件会受到影响。
另一方面,假设您有一个包含十几个小部件的布局,但只有一个小部件正在制作动画 - 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的回答我找到了解决方案:
public static void setProgress(MotionLayout motionLayout, float progress) {
if (ViewCompat.isLaidOut(motionLayout)) {
motionLayout.setProgress(progress);
} else {
motionLayout.post(() -> motionLayout.setProgress(progress));
}
}
解决方案
这很奇怪,布局检查器显示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>
推荐阅读
- amazon-web-services - 找不到模型 PipelineModel
- sql - oracle sql 仅在两个表之间返回不匹配的结果
- c++ - 什么样的设计模式可以在不更改签名的情况下访问 lambda 函数中的封装范围变量?
- python - 在 VS Code 中使用 Selenium 时无法解析导入“webdriver_manager.chrome”
- reactjs - safari 和 ios 平台上的 favicon 未出现
- sql - 使用 K8s 加载到 SQL 数据库 - 重复数据?
- f# - Visual Studio 2019 F# NU1101 找不到包 FSharp.core
- r - R 中的调查数据:未选择的响应频率
- javascript - 如果我定义了一个异步函数并在主体中使用了await,那么在调用该函数时是否需要使用await?
- c++ - 使用 argv 的分段错误