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

4

我正在尝试使用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
1个回答

0

MaterialContainerTransform转换,在底层使用一个drawable来在两个状态之间进行动画。默认情况下,将该drawable作为覆盖添加到android.R.id.content(根视图)。

我的猜测是,由于两个drawable都被添加为同一视图的覆盖层,因此其中一个会被完全覆盖。我需要查看视图覆盖系统以确保。

但是,在MaterialContainerTransform中有一种方法可以让您设置哪个视图将使用该drawable作为覆盖层。

  /**
   * 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;
}

使用此方法为其中一个转换设置不同的“绘图视图”,我认为这将解决问题。

1
是的,现在它完美地工作了!非常感谢您的建议。最终我只是在第二个MaterialContainerTransform中添加了一个“绘图视图”,但这已经足够了。 - Gori

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接