【问题标题】:Android shared element transition between two activities does not work两个活动之间的Android共享元素转换不起作用
【发布时间】:2015-10-26 13:21:15
【问题描述】:

在我的应用程序中,我尝试在活动之间使用新引入的元素共享。如果共享元素具有固定位置(例如android:layout_gravity="top"),那么一切都像魅力一样,但是当视图被锚定时问题就来了。

我的第一个活动如下所示:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:auto="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        ...
    </android.support.design.widget.AppBarLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/play_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="24dp"/>

</android.support.design.widget.CoordinatorLayout>

我的第二个活动看起来像这样

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:auto="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:elevation="10dp"
        android:src="@drawable/ic_action_play"
        auto:layout_anchor="@+id/appbar"
        android:transitionName="fab_button"
        auto:layout_anchorGravity="bottom|right" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="192dp">
        ...
    </android.support.design.widget.AppBarLayout>

    ...

</android.support.design.widget.CoordinatorLayout>

我使用的代码如下:

Intent intent = ...;
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(this, view, "fab_button");
startActivity(intent, options.toBundle());

如果我使用layout_anchorlayout_anchorGravity 属性,则两个FAB 之间的过渡是在没有动画的情况下完成的。如果第二个 FAB 位置固定,则可以完美运行。我做错了什么?

【问题讨论】:

  • 我认为你没有做错什么。对于这两个活动,我的应用程序与您的应用程序非常相似,我也有同样的问题。我认为共享元素过渡框架无法获取锚定元素的位置......但我希望有人能找到解决方案
  • 我看到有些应用程序的这种行为效果很好。我猜他们正在做一些自定义逻辑来实现这种效果。
  • 感谢您的报告,为此我已经为这个问题挠了好几个小时。当我删除 app:layout_anchor 时,过渡很顺利,因为它需要。怎么可能这件事这么难发现,还没有报道……加油……
  • 您应该在 Support Libs 下的 android's bug tracker 中报告此错误

标签: android android-design-library floating-action-button


【解决方案1】:

这可能有点晚了,但我找到了解决这个问题的方法。您必须将共享元素包装到布局中,并将锚点放在该布局上:

<FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        auto:layout_anchor="@+id/appbar"
        auto:layout_anchorGravity="bottom|right">

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:elevation="10dp"
            android:src="@drawable/ic_action_play"
            android:transitionName="fab_button" />

<FrameLayout/>

【讨论】:

  • 这是 coordinatorLayout + 共享转换中的错误吗?但它就像一个魅力:)
【解决方案2】:

活动共享元素转换:

按照步骤操作。

  1. Enable Window Content Transitions
  2. Assign a Common Transition Name
  3. Start Activity
  4. Multiple Shared Elements
  5. Customizing Shared Elements Transition

我认为你没有遵循第二步。你在第二个Activity 中给出了android:transitionName="fab_button",但在第一个Activity 中没有给出。

XML 的第一个 Activity(已添加 android:transitionName="fab_button"):

<android.support.design.widget.FloatingActionButton

        android:transitionName="fab_button"

        android:id="@+id/play_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="24dp"/>

以上链接可能会对您有所帮助。

【讨论】:

  • 不,不是这样。第一个活动视图是一个图像视图,目标是一个锚定的 FAB。如果我将目标 FAB 设为非锚定,则一切正常。
【解决方案3】:

您在接收活动时也应该有转换名称。检查我的代码。

第一个活动

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_register"
        android:transitionName="@string/transition"/>

</android.support.design.widget.CoordinatorLayout>

第二个活动 xml

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:transitionName="@string/transition"
tools:context="com.example.shoppingappdemo.RegiserActivity">

【讨论】:

  • 和我在回答中给出的一样。
  • 对此感到抱歉..实际上我的网络速度很慢,所以在我发布答案后,我显示 urs...对不起
  • 这不是问题。请阅读问题,如果目标 FAB 未锚定,一切正常。问题似乎出在目标 FAB 被锚定时。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2019-01-20
相关资源
最近更新 更多