Anr*_*ian 7 android android-layout android-constraintlayout
我有两个小部件的 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>
Run Code Online (Sandbox Code Playgroud)
场景文件:
<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>
Run Code Online (Sandbox Code Playgroud)
然后在活动中我设置了进度:
//onCreate, etc..
MotionLayout motionLayout = findViewById(R.id.motion_layout);
motionLayout.setProgress(0.5f);
Run Code Online (Sandbox Code Playgroud)
动态的观点是右边的位置,但静态视图失去了所有的限制,但它不是由现场的影响,如所描述这里,只有在场景文件中描述的小部件应受到影响。
另一方面,假设您有一个包含十几个小部件的布局,但只有一个您正在制作动画?-?MotionScene 中的 ConstraintSet 只需要包含该小部件的约束。
库版本:
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:2.0.0-alpha2'
Run Code Online (Sandbox Code Playgroud)
示例项目在这里: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));
}
}
Run Code Online (Sandbox Code Playgroud)
这很奇怪,布局检查器显示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);
Run Code Online (Sandbox Code Playgroud)
并且 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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5803 次 |
| 最近记录: |