两个活动之间的Android共享元素过渡不起作用 [英] Android shared element transition between two activities does not work

查看:119
本文介绍了两个活动之间的Android共享元素过渡不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我试图使用新引入的活动之间的元素共享.如果共享元素的位置固定(例如android:layout_gravity="top"),则所有内容都会像超级按钮一样工作,但问题是在锚定视图时出现的.

In my app I'm trying to use the newly introduced element sharing between activities. Everything works like a charm if the shared element is with fixed position (e.g. android:layout_gravity="top") but the problem comes when the view is anchored.

我的第一个活动是这样的:

My first activity looks like this:

<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>

我的第二项活动是这样的

My second activity looks like this

<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的位置固定,则可以正常工作.我在做什么错了?

If I use the layout_anchor and layout_anchorGravity attributes the transition between the two FABs is done with no animation. If the second FAB is with fixed position, it works perfectly. What am I doing wrong?

推荐答案

这可能会有点晚,但是我找到了解决此问题的方法.您必须将共享元素包装到一个布局中,然后将锚点放置在该布局上:

This might be a bit late, but I found a way around the issue. You have to wrap your shared element into a layout, and put the anchor on that layout:

<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/>

这篇关于两个活动之间的Android共享元素过渡不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆