如何 运行 多个 MaterialContainerTransform 同时转换?

How to run multiple MaterialContainerTransform transitions simultaneously?

我正在尝试使用 MaterialContainerTransform 转换同时为两组视图设置动画。但是,使用下面的代码,只有第一个转换 运行 正确,而第二个从开始状态跳到结束状态没有动画。单独测试时,两者都应该 运行 。在网上搜索了好几个小时后,我至今仍找不到任何解决办法。如有任何帮助,我们将不胜感激!

val transform = TransitionSet().apply {
    ordering = TransitionSet.ORDERING_TOGETHER

    addTransition(MaterialContainerTransform().apply {
        startView = startViewA
        endView = endViewA

        addTarget(endViewA)

        pathMotion = MaterialArcMotion()
        scrimColor = Color.TRANSPARENT
    })

    addTransition(MaterialContainerTransform().apply {
        startView = startViewB
        endView = endViewB

        addTarget(endViewB)

        pathMotion = MaterialArcMotion()
        scrimColor = Color.TRANSPARENT
    })
}

TransitionManager.beginDelayedTransition(viewContainer, transform)

startViewA?.visibility = View.GONE
endViewA?.visibility = View.VISIBLE
endViewB?.visibility = View.INVISIBLE
startViewB?.visibility = View.VISIBLE

MaterialContainerTransform 转换,在引擎盖下,使用可绘制对象在两种状态之间进行动画处理。默认情况下,可绘制对象作为叠加层添加到 android.R.id.content(根视图)。

我的猜测是,由于两个可绘制对象都作为叠加层添加到同一视图,所以一个被完全覆盖了。我需要查看视图覆盖系统以确保。

但是 MaterialContainerTransform 中有一个方法可以让您设置哪个视图将使用可绘制对象作为叠加层。

  /**
   * Set the id of the View whose overlay this transition will be added to.
   *
   * <p>This can be used to limit the bounds of the animation (including the background scrim) to
   * the bounds of the provided drawing view, and also have the animation drawn at the relative
   * z-order of the drawing view.
   *
   * <p>By default, the {@code drawingViewId} will be {@code android.R.id.content}. Additionally, if
   * {@code drawingViewId} is the same as the end View's id, {@code MaterialContainerTransform} will
   * add the transition's drawable to the {@code drawingViewId}'s parent instead.
   *
   * <p>If the drawing view cannot be found during the initialization of the {@code
   * MaterialContainerTransform}, then an {@link IllegalArgumentException} will be thrown.
   */
public void setDrawingViewId(@IdRes int drawingViewId) {
this.drawingViewId = drawingViewId;
}

使用此方法为您的其中一个过渡设置不同的 'drawing view',我认为这可以解决问题。