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

标签 android material-components-android android-transitions

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

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默认情况下( Root View )。
我的猜测是,由于两个可绘制对象都作为叠加层添加到同一 View 中,因此其中一个被完全覆盖。我需要看一下 View 覆盖系统以确保。
但是 MaterialContainerTransform 中有一个方法可以让您设置哪个 View 将使用可绘制对象作为叠加层。

  /**
   * 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;
}
使用此方法为您的一个转换设置不同的“绘图 View ”,我认为这可以解决它。

关于android - 如何同时运行多个 MaterialContainerTransform 转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64018229/

相关文章:

android - 在退出转换之前更新源 Activity

android - CollapsingToolbarLayout 标题作为共享元素过渡

android - 将幻灯片过渡应用于 ViewGroup 的子级

android - GetTY allState 始终返回空闲状态

Android - Gradle 同步失败 : org/jetbrains/kotlin/kapt/idea/KaptGradleModelorg/jetbrains/kotlin/kapt/idea/KaptGradleModel

android - 我的自定义 Open Graph 对象的自定义属性未显示在共享对话框中

android - 如何升级 meteor 应用程序

android - 创建自定义按钮类以在 Android 中设置样式

android - MaterialDatePicker 显示当前日期而不是需要

Android 浅色/深色主题操作栏文本