问题描述
在给定的布局中,我有3个状态,我想为这些状态之间的任何转换使用相同的插值器。如何在所有转换中(使用XML或代码)定义相同的插值器,而不必重复以下步骤?
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:constraintSetStart="@id/state_a"
motion:constraintSetEnd="@id/state_b"
motion:motionInterpolator="easeOut"
motion:duration="250"/>
<Transition
motion:constraintSetStart="@id/state_a"
motion:constraintSetEnd="@id/state_c"
motion:motionInterpolator="easeOut"
motion:duration="250"/>
<Transition
motion:constraintSetStart="@id/state_b"
motion:constraintSetEnd="@id/state_c"
motion:motionInterpolator="easeOut"
motion:duration="250"/>
<ConstraintSet android:id="@+id/state_a">...</ConstraintSet>
<ConstraintSet android:id="@+id/state_b">...</ConstraintSet>
<ConstraintSet android:id="@+id/state_c">...</ConstraintSet>
</MotionScene>
解决方法
这是定义所有三个过渡共同的插值器或持续时间的方法:
<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:motion="http://schemas.android.com/apk/res-auto">
<Transition
motion:motionInterpolator="easeOut"
motion:duration="250" />
<Transition
motion:constraintSetStart="@id/state_a"
motion:constraintSetEnd="@id/state_b" />
<Transition
motion:constraintSetStart="@id/state_a"
motion:constraintSetEnd="@id/state_c" />
<Transition
motion:constraintSetStart="@id/state_b"
motion:constraintSetEnd="@id/state_c" />
<ConstraintSet android:id="@+id/state_a">...</ConstraintSet>
<ConstraintSet android:id="@+id/state_b">...</ConstraintSet>
<ConstraintSet android:id="@+id/state_c">...</ConstraintSet>
</MotionScene>
查看当前的MotionLayout源(v2.0.1),未指定Transition
和constraintSetStart
的{{1}}元素被视为抽象,并且其属性将成为MotionScene中所有“转场”的默认设置。
constraintSetEnd
当MotionScene中的过渡完全抽象(未指定开始或结束)时,它将成为public static class Transition {
private boolean mIsAbstract = false;
private int mConstraintSetEnd = -1;
private int mConstraintSetStart = -1;
// ...
// This constructor creates a Transition based on another Transition
Transition(MotionScene motionScene,MotionScene.Transition global) {
if (global != null) {
this.mDefaultInterpolator = global.mDefaultInterpolator;
this.mDuration = global.mDuration;
// ...
}
}
private void fill(MotionScene motionScene,Context context,TypedArray a) {
// Parse attributes from TypedArray,constraintSetStart being one of them
if (this.mConstraintSetStart == -1) {
this.mIsAbstract = true;
}
}
// ...
}
:
mDefaultTransition
它按照我描述的那样工作,但是在当前文档中没有任何引用。不幸的是,我无法链接到源代码,因为AOSP中没有最新版本(请参见this issue)。查看此代码的唯一方法是通过IntelliJ的反编译器。
,您可以定义“抽象”过渡。在beta4的发行说明中提到 https://developer.android.com/jetpack/androidx/releases/constraintlayout#2.0.0-beta4
注意,它不会成为其他基础
您可以在https://github.com/androidx/constraintlayout
中找到源代码不幸的是,没有很好的方法来记录不放东西的功能。